I need a little XSLT help. Couldn't figure out why the actual output is different from my expected output. Any help much appreciated!
XML
<?xml version="1.0"?>
<a>
<b c="d"/>
<b c="d"/>
<b c="d"/>
</a>
XSL
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="foo">
<xsl:param name="content"></xsl:param>
<xsl:value-of select="$content"></xsl:value-of>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="foo">
<xsl:with-param name="content">
<xsl:for-each select="a/b">
<e>
<xsl:value-of select="@c" />
</e>
</xsl:for-each>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
Actual Output
<?xml version="1.0"?>
ddd
Desired Output
<?xml version="1.0"?>
<e>d</e>
<e>d</e>
<e>d</e>
Note: Calling the template is mandatory. In my situation the template does more with extension functions.
Contrary to what ABach says, your
xsl:param
is fine. The only thing you need to change is yourxsl:value-of
. It should be axsl:copy-of
:You're very close; you've just mixed up relative positioning and correct parameter usage within templates. Here's a slightly revised answer.
When this XSLT:
...is applied to the original XML:
...the desired result is produced:
In particular, notice the correct usage of
<xsl:param>
to include nodes based on their relative position. In your case, you are telling the XSLT parser to output the text values of the parameter that you're passing, rather than altering the nodes' contents in the way you want.