如何在XSLT中的两个模板之间传递变量

时间:2023-01-12 22:30:00

How to pass variables between two templates in XSLT.

如何在XSLT中的两个模板之间传递变量。

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

我不能使用全局变量,因为变量的值取决于评估中的当前节点。

Say I have XSLT of sort:

说我有XSLT的排序:

<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.

这会产生错误,因为在第二个模板中无法访问$ pr-pos。

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

如何将变量pr-pos'值传递给其他模板?我怎样才能做到这一点?

1 个解决方案

#1


10  

<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>
      ..
      ..

#1


10  

<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>
      ..
      ..