xslt 1.0 - How to pass dynamic separator in key XSLT1.0 -
how pass dynamic separator in , here passed '|' static , when declare variable in xsl , used place gives error.
<xsl:variable name="separator" select="'|'"/> <xsl:key name="key-before" match="result" use="substring-before(store, $separator)"/>
input 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>
what ask not possible (in xslt 1.0). neither match
nor use
attribute of xsl:key
can contain reference variable.
Comments
Post a Comment