Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables between two templates in XSLT

Tags:

xml

xslt

xpath

How to pass variables between two templates in XSLT.

I cannot use global variable because value of variable is dependent on current node under evaluation.

Say I have XSLT of sort:

<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 error as $pr-pos is not accessible in second template.

How do I pass variable pr-pos' value to other template? How can I do this?

like image 754
Harshdeep Avatar asked Jun 30 '12 19:06

Harshdeep


1 Answers

<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>
      ..
      ..
like image 122
Don Roby Avatar answered Nov 14 '22 19:11

Don Roby