I'm using the generate-id()
function inside a predicate to compare with a previously generated id. But this does not work and I can't figure out why.
I've created a sample XSLT with input and expected output. Yes I know - this code does seems weird and does not make sense, it is just to find out, why the generate-id()
does not work here.
The problem line is the following:
<xsl:value-of select="a[generate-id(current())=$a_id]/text()"/>
This does not work. Here the complete XML input, XSLT and expected output.
XML Input:
<root>
<test>
<a>1</a>
<a>2</a>
<a>3</a>
</test>
</root>
Expected Output:
<root>
<test>
<a>1</a>
</test>
<test>
<a>2</a>
</test>
<test>
<a>3</a>
</test>
</root>
Result Output (the values are not found with the id search):
<root>
<test>
<a/>
</test>
<test>
<a/>
</test>
<test>
<a/>
</test>
</root>
Here is the XSLT. The problem is in the last template:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="yes"></xsl:output>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates select="test"/>
</xsl:copy>
</xsl:template>
<xsl:template match="test">
<xsl:for-each select="a">
<xsl:apply-templates select=".." mode="output">
<xsl:with-param name="a_id" select="generate-id()"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="test" mode="output">
<xsl:param name="a_id"/>
<xsl:copy>
<xsl:element name="a">
<!-- Here the passed a_id cannot be found -->
<xsl:value-of select="a[generate-id(current())=$a_id]/text()"/>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>