输入XML
如何传递动态分隔符,这里我传递'|'作为静态,当我在xsl中声明变量并使用这个地方时,它会给出错误。
<xsl:variable name="separator" select="'|'"/>
<xsl:key name="key-before" match="result" use="substring-before(Store, $separator)"/>
输入XML
<?xml version="1.0" encoding="iso-8859-1"?>
<results>
<result>
<Store>0180|1</Store>
</result>
<result>
<Store>0180|2</Store>
</result>
<result>
<Store>0181</Store>
</result>
<result>
<Store>0183</Store>
</result>
<result>
<Store>abc</Store>
</result>
<result>
<Store>def</Store>
</result>
<result>
<Store>0181|2</Store>
</result>
<result>
<Store>0180|3</Store>
</result>
<result>
<Store>0181|1</Store>
</result>
</results>
XSLT: <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="key-direction" select="'p'"/>
<xsl:param name="separator" select="results/sep"/>
<xsl:variable name="demo" select="substring-before(Store,$separator)"/>
<xsl:key name="group-before" match="/results/result" use="substring-before(Store,$demo)" />
<xsl:key name="group-after" match="/results/result" use="substring-before(Store,$demo)" />
<xsl:template match="/results">
<xsl:choose>
<xsl:when test="$key-direction='p'">
<xsl:apply-templates select="result[generate-id() = generate-id(key('group-before', substring-before(Store,$separator))[1])]"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="result[generate-id() = generate-id(key('group-after', substring-before(Store,$separator))[1])]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="result">
<xsl:choose>
<xsl:when test="$key-direction='p'">
<xsl:for-each select="key('group-before', substring-before(Store,$separator))">
<xsl:choose>
<xsl:when test="contains(Store,'|')">
<td>
<xsl:value-of select="substring-before(Store,$separator)"/>
<xsl:value-of select="'|'"/>
<xsl:value-of select="position()"/>
</td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="Store"/></td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="key('group-after', substring-after(Store,$separator))">
<xsl:choose>
<xsl:when test="contains(Store,'|')">
<td>
<xsl:value-of select="substring-after(Store,$separator)"/>
<xsl:value-of select="'|'"/>
<xsl:value-of select="position()"/>
</td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="Store"/></td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
您所要求的是不可能的(在XSLT 1.0中)。xsl:key
的match
和use
属性都不能包含对变量的引用