XML Schema 早于 XSLT 2.0,因此 XSLT 2.0 可以通过tokenize()
.
XSLT 1.0 早于 XML Schema,因此您需要一个递归模板调用来分割字符串:
T:\ftemp>type tokenize.xml
<?xml version="1.0" encoding="UTF-8"?>
<data>
<!-- this list has 6 items -->
<list>this is a list of strings</list>
</data>
T:\ftemp>xslt tokenize.xml tokenize.xsl
this,is,a,list,of,strings
T:\ftemp>type tokenize.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:template match="data">
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="normalize-space(list)"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string,' ')">
<xsl:value-of select="substring-before($string,' ')"/>
<xsl:text>,</xsl:text>
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="substring-after($string,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
T:\ftemp>xslt2 tokenize.xml tokenize2.xsl
this,is,a,list,of,strings
T:\ftemp>type tokenize2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="text"/>
<xsl:template match="data">
<xsl:value-of select="tokenize(list,'\s+')" separator=","/>
</xsl:template>
</xsl:stylesheet>
T:\ftemp>