【问题标题】:XSLT - split string on every nth characterXSLT - 每第 n 个字符分割字符串
【发布时间】:2013-08-02 11:15:37
【问题描述】:

大家好

我想创建一个 xslt 2.0 样式表来执行以下操作:

拆分下面的xml元素值:

<sample>Please Move the asset as below

Asset Name: My Monitor 40 inch_123456789123
Asset Serial Number: 123456789123

Current Details:
Costcenter: 1234-123456 MY COST CENTRE
Location: 12 - 1234 - 1234 - MY COST ADDRESS,12 MY STR.,10TH FLOOR,,CITY,Country Name

Destination Details: 
Cost Center: 1234-12345 : 5678-91234 Some Place</sample>

在每 70 个字符上,然后将前 9 个结果分别分配给一个固定的、配置的新元素名称,并丢弃任何剩余的匹配项。示例:

<humpty>first 70chars</humpty>
<dumpty>second70chars</dumpty>
<sat>third70chars</sat>
etc...

我曾考虑过使用 tokenize 但被卡住了,因为它需要一个字符串模式来匹配。我考虑过使用子字符串,但我不确定格式。

感谢任何建议!

【问题讨论】:

  • 您可以使用analyze-string。但是我不确定你想从哪里获得新的元素名称,如 humpty from
  • 谢谢马丁。新元素名称将在样式表中预先配置并始终保持不变。为了这个例子,我使用了humpty 和其他人。

标签: xslt xslt-2.0


【解决方案1】:

如果您知道元素名称并且只想提取 70 个字符的子字符串,那么 http://www.w3.org/TR/xpath/#function-substring 应该这样做:

<xsl:template match="sample">
  <humpty><xsl:value-of select="substring(., 1, 70)"/></humpty>
  <dumpty><xsl:value-of select="substring(., 71, 70)"/></dumpty>
  <sat><xsl:value-of select="substring(., 141, 70)"/></sat>
  ...
</xsl:template>

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 2022-01-18
    • 2019-01-12
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2023-03-08
    相关资源
    最近更新 更多