如何拆分XSLT 1.0中的节点值?
- <mark>1,2</mark>
我需要在for循环中执行一些操作,其中每个值都是split的输出.
< xsl:for-each select =“”>
< /的xsl:for-每个>
怎么办?
I. XSLT 1.0解决方案:
以下是XSLT 1.0中仅使用xxx:node-set()扩展功能的一种方法:
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
- <xsl:output omit-xml-declaration="yes" indent="yes"/>
- <xsl:template match="mark">
- <xsl:variable name="vrtfSplit">
- <xsl:apply-templates/>
- </xsl:variable>
- <xsl:for-each select="ext:node-set($vrtfSplit)/*">
- <processedItem>
- <xsl:value-of select="10 * ."/>
- </processedItem>
- </xsl:for-each>
- </xsl:template>
- <xsl:template match="text()" name="split">
- <xsl:param name="pText" select="."/>
- <xsl:if test="string-length($pText) >0">
- <item>
- <xsl:value-of select=
- "substring-before(concat($pText,','),')"/>
- </item>
- <xsl:call-template name="split">
- <xsl:with-param name="pText" select=
- "substring-after($pText,')"/>
- </xsl:call-template>
- </xsl:if>
- </xsl:template>
- </xsl:stylesheet>
当此转换应用于以下XML文档时:
- <mark>1,2,3,4,5</mark>
产生想要的,正确的输出(每个项目乘以10):
- <processedItem>10</processedItem>
- <processedItem>20</processedItem>
- <processedItem>30</processedItem>
- <processedItem>40</processedItem>
- <processedItem>50</processedItem>
II. XSLT 2.0解决方案:
- <xsl:stylesheet version="2.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
- exclude-result-prefixes="xs">
- <xsl:output omit-xml-declaration="yes" indent="yes"/>
- <xsl:template match="mark">
- <xsl:for-each select="tokenize(.,')">
- <processedItem>
- <xsl:sequence select="10*xs:integer(.)"/>
- </processedItem>
- </xsl:for-each>
- </xsl:template>
- </xsl:stylesheet>