如何循环我在XSLT 1.0中作为参数传递的逗号分隔字符串?EX-
<xsl:param name="UID">1,4,7,9</xsl:param>
Run Code Online (Sandbox Code Playgroud)
我需要循环上面的UID参数并从我的XML文件中的每个UID中收集节点
Tom*_*lak 21
Vanilla XSLT 1.0可以通过递归解决这个问题.
<xsl:template name="split">
<xsl:param name="list" select="''" />
<xsl:param name="separator" select="','" />
<xsl:if test="not($list = '' or $separator = '')">
<xsl:variable name="head" select="substring-before(concat($list, $separator), $separator)" />
<xsl:variable name="tail" select="substring-after($list, $separator)" />
<!-- insert payload function here -->
<xsl:call-template name="split">
<xsl:with-param name="list" select="$tail" />
<xsl:with-param name="separator" select="$separator" />
</xsl:call-template>
</xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
有预先构建的扩展库可以进行字符串标记化(例如,EXSLT有一个模板),但我怀疑这是非常必要的.
这是使用FXSLstr-split-to-words模板的XSLT 1.0 解决方案。
请注意,此模板允许分割多个分隔符(作为单独的参数字符串传递),因此1,4 7;9使用此解决方案即使分割也不会出现任何问题。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="', ;	 '"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于以下 XML 文档时:
<x>1,4,7,9</x>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
<word>1</word>
<word>4</word>
<word>7</word>
<word>9</word>
Run Code Online (Sandbox Code Playgroud)