XSLT - 在每个第n个字符上拆分字符串

时间:2013-08-02 11:15:37

标签: xslt xslt-2.0

大家好

我想创建一个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但是卡住了,因为它需要一个匹配的字符串模式。我想过使用子字符串,但我不确定格式。

任何建议表示赞赏!

1 个答案:

答案 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>
相关问题