XSLT section restriction to single node

Is there a way to limit the XSLT section to a single node so that the entire node path is not needed every time?

For instance...

Name: <xsl:value-of select="/root/item[@attrib=1]/name"/>
Age: <xsl:value-of select="/root/item[@attrib=1]/age"/>

This can be done with each team, but I am convinced that they 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 think I'm asking if there is an XSLT equivalent of the VB.NET With command?

I would prefer to avoid xsl: template for readability, since the XSLT file in question is large, but would gladly accept if this is the only way to do this. And if so, what syntax calls a specific template based on a specific node?

Update

In response to the answer by @javram, individual patterns can be mapped based on specific 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>
+5
source share
4 answers

- :

<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>
+2

:

<xsl:variable name="foo" select="/root/item[@attrib=1]" />

<xsl:value-of select="$foo/name" />
<xsl:value-of select="$foo/age" />
+2

XSLT 2.0 :

<xsl:value-of select='/root/item[@attrib=1]/
                       concat("Name: ", name, " Age: ", age)'/>
0

:

<xsl:for-each select="/root/item[@attrib=1]"/>
  Name: <xsl:value-of select="name"/>
  Age: <xsl:value-of select="age"/>
</xsl:for-each>

( node, ).

:

<xsl:for-each select="(/root/item[@attrib=1])[1]"/>
  Name: <xsl:value-of select="name"/>
  Age: <xsl:value-of select="age"/>
</xsl:for-each>

is reset to the first (possibly only) node match and is equivalent to the VB.NET operator Withas you wish.

0
source

All Articles