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>
xsl:for-each
as you are doing here. In this case, it functions fine in a "with" sort of way. People counsel against usingxsl:for-each
to explicitly loop over something when XSLT provides better ways to do the equivalent, such as letting templates apply and match. - user663031