Reputation: 2066
In XSLT 1.0, what is the shortest/cleanest/recommended way to pass the current context node to a called template and have that node become the context node inside the called template?
It would be nice (it would, right?) if a template with no xsl:param and called by an empty call-template would simply pick up the caller's context node, but the best I can think of is this:
<xsl:call-template name="sub">
<xsl:with-param name="context" select="." />
</xsl:call-template>
with
<xsl:template name="sub">
<xsl:param name="context" />
<xsl:for-each select="$context">
</xsl:for-each>
</xsl:template>
Upvotes: 25
Views: 21907
Reputation: 12154
Just otherway of explaining what Dimitre said.
When you call a template from a node, you are already there in that node,
example:
assume this code:
<xsl:template match="MyElement">
<xsl:call-template name="XYZ"/>
</xsl:template>
<xsl:template name="XYZ>
<xsl:value-of select="."/>
</xsl>
The above code is as good as writing:
<xsl:template match="MyElement">
<xsl:value-of select="."/>
</xsl:template>
You can use for-each loop in the called template as well. :)
But just be sure where you exactly are ..
Upvotes: 6
Reputation: 243459
It would be nice (it would, right?) if a template with no
xsl:param
and called by an emptycall-template
would simply pick up the caller's context node.
This is exactly how xsl:call-template
is defined in the W3C XSLT 1.0 (and 2.0) specification, and implemented by any compliant XSLT processor.
Here is a small example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="a">
<xsl:call-template name="currentName"/>
</xsl:template>
<xsl:template name="currentName">
Name: <xsl:value-of select="name(.)"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<t>
<a/>
</t>
the wanted, correct result is produced:
Name: a
Upvotes: 34