Is there a way of restricting a section of XSLT to a single node, so that the entire path of the node is not necessary each time?
For example...
Name: <xsl:value-of select="/root/item[@attrib=1]/name"/>
Age: <xsl:value-of select="/root/item[@attrib=1]/age"/>
This could be done via a for-each command, but I am led to believe that these should be avoided if at all possible...
<xsl:for-each select="/root/item[@attrib=1]"/>
Name: <xsl:value-of select="name"/>
Age: <xsl:value-of select="age"/>
</xsl:for-each>
I guess I'm asking if there is an XSLT equivalent of the VB.NET With command?
I would prefer to avoid xsl:template for readability, as the XSLT file in question is large, but happy to accept if that is the only way to do it. And if so, what is the syntax to call a particular template based on specific node?
Update
In follow up to the answer by @javram, it is possible to match separate templates based on particular attributes / nodes.
<xsl:apply-templates select="/root/item[@attrib=1]"/>
<xsl:apply-templates select="/root/item[@attrib=2]"/>
<xsl:template match="/root/item[@attrib=1]">
Name: <xsl:value-of select="name"/>
Age: <xsl:value-of select="age"/>
</xsl:template>
<xsl:template match="/root/item[@attrib=2]">
Foo: <xsl:value-of select="foo"/>
</xsl:template>
The correct way would be to use a template:
<xsl:apply-templates select="/root/item[@attrib=1]"/>
.
.
.
<xsl:template match="/root/item">
Name: <xsl:value-of select="name"/>
Age: <xsl:value-of select="age"/>
</xsl:template>
You may use variables:
<xsl:variable name="foo" select="/root/item[@attrib=1]" />
<xsl:value-of select="$foo/name" />
<xsl:value-of select="$foo/age" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With