有几种方法可以做到这一点。
I.在XSLT 1.0 使用递归所谓命名模板这种转化:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[not(name()='tags')]">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@tags">
<xsl:call-template name="tokenize">
<xsl:with-param name="pText"
select="concat(., ',')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="pText"/>
<xsl:if test="string-length($pText)">
<tag>
<xsl:value-of select=
"substring-before($pText, ',')"/>
</tag>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
当在最初提供的XML文档施加(校正为良好的形成):
<post title="Hello World"
tags="Test,Hello,World" />
产生所需的结果:
<post>
<title>Hello World</title>
<tag>Test</tag>
<tag>Hello</tag>
<tag>World</tag>
</post>
二,从FXSL 1.x的
这里使用str-split-to-words
模板/功能FXSL提供标记化功能:
<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:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[not(name()='tags')]">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@tags">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="."/>
<xsl:with-param name="pDelimiters"
select="','"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
</xsl:template>
<xsl:template match="word">
<tag>
<xsl:value-of select="."/>
</tag>
</xsl:template>
</xsl:stylesheet>
当作为前对相同的XML文档应用中,相同的正确的输出产生 。
三,使用来自XSLT 2.0转换的XPath 2.0标准函数tokenize()
这是最简单的方法 - 如果可以使用XSLT 2.0处理器。
下面的XSLT变换2.0:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[not(name()='tags')]">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@tags">
<xsl:for-each select="tokenize(.,',')">
<tag><xsl:value-of select="."/></tag>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
当对相同的XML文档施加再次产生希望的结果。
你错配了“和 ”并留下了“ 未闭合。 – Sparr 2009-02-13 02:13:23