Ket*_*tan 25 string xslt split
如何根据某些分隔符拆分字符串?
给定一个字符串Topic1,Topic2,Topic3,我想基于,生成来分割字符串:
Topic1 Topic2 Topic3
Run Code Online (Sandbox Code Playgroud)
小智 37
在XSLT 1.0中,您必须构建一个递归模板.这个样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="','"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<item>
<xsl:value-of select="normalize-space($text)"/>
</item>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</item>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输入:
<root>
<text>Item1, Item2, Item3</text>
</root>
Run Code Online (Sandbox Code Playgroud)
输出:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
Run Code Online (Sandbox Code Playgroud)
在XSLT 2.0中,您拥有tokenize()核心功能.那么,这个样式表:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="separator" select="','"/>
<xsl:for-each select="tokenize(.,$separator)">
<item>
<xsl:value-of select="normalize-space(.)"/>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
结果:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
Run Code Online (Sandbox Code Playgroud)