Ricky Stam
Ricky Stam

Reputation: 2126

XSL call template with dynamic nodes

Hello i need to achieve the below functionality in my XSL but it seems i'm stuck... Any help will be much appreciated.

Please see my comments inside the below code snippet.

<xsl:template name="/">
<xsl:call-template name="looptemplate">
      <xsl:with-param name="x" select="1"/>
      <xsl:with-param name="max" select="10"/>
</xsl:call-template>
</xsl:template>

<xsl:template name=" looptemplate">
<xsl:param name="x"/>
<xsl:param name="max"/>

    <xsl:call-template name="TemplateToCall">
        <xsl:with-param name="nodePath" select="a/b$i"></xsl:with-param>

        <!--
        Get dynamically root nodes
        a/b1, a/b2, a/b3 etc
        -->

    </xsl:call-template>
                  <!--
                Loop again until x reaches max
               -->
</xsl:template>

<xsl:template name="TemplateToCall">
<xsl:param name="nodePath"/>

<xsl:for-each select="$nodePath">
    <xsl:value-of select="value1"/>, <xsl:value-of select="value2"/>
</xsl:for-each>
</xsl:template>

Upvotes: 3

Views: 3165

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

You can't build an XPath as a string and evaluate it dynamically like that (at least not in plain XSLT 1.0 or 2.0, there will be an xsl:evaluate instruction in XSLT 3.0), but you could do something like

<xsl:call-template name="TemplateToCall">
    <xsl:with-param name="nodes" select="a/*[local-name() = concat('b', $i)]"/>

and then in the called template

<xsl:template name="TemplateToCall">
    <xsl:param name="nodes"/>

    <xsl:for-each select="$nodes">

Upvotes: 4

Related Questions