How to pass variables between two templates in XSLT

How to pass variables between two templates in XSLT.

I cannot use a global variable since the value of the variable depends on the current evaluation of the node.

Let's say I have an XSLT kind:

<xsl:template match="product">
<xsl:variable name="pr-pos" select="count(./preceding-sibling::product)+1"/>
..
..
..
<xsl:apply-templates select="countries/country"/>
</xsl:template>

<xsl:template match="countries/country">
<tr id="country-id">
  <td><a href="#" class="action" id="{concat('a-',$pr-pos)}">+</a></td>
..
..

This gives an error because $ pr-pos is not available in the second pattern.

How to pass the value of the pr-pos variable to another template? How can i do this?

+5
source share
1 answer
<xsl:template match="product">
    <xsl:variable name="pr-pos" select="count(./preceding-sibling::product)+1"/>
    ..
    ..
    ..
    <xsl:apply-templates select="countries/country">
       <xsl:with-param name="pr-pos" select="$pr-pos" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="countries/country">
  <xsl:param name="pr-pos" />
    <tr id="country-id">
      <td><a href="#" class="action" id="{concat('a-',$pr-pos)}">+</a></td>
      ..
      ..
+10
source

All Articles