Dennis Buttery
Dennis Buttery

Reputation: 13

XSLT using variable in for-each select

I am trying to supply a dynamic name to a for-each select and cannot seem to get it to work.

My template is intended to allow a specific tag name to be passed in and then those nodes are selected from the xml and used for processing.

<xsl:param name="tagName" select="'DefaultTag'" />

<xsl:variable name="DataSource" select="document('../xml/datasource.xml')"/>

<xsl:template name="ProcessData">
    <xsl:for-each select="$DataSource//$tagName/DataValue" >

        ... Data operations here ...

    </xsl:for-each>
</xsl:template>

If I replace the $tagName in the for-each select with a hard-coded value then it works (for that value).

How do I get the variable substitution to happen using the param value?

Thanks, -Dennis

Upvotes: 0

Views: 652

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122394

You can't use variables like this in xpath to generate "dynamic" expressions, but in this case the variable part is just a single element name so you can filter using a predicate:

$DataSource//*[local-name() = $tagName]/DataValue

Upvotes: 4

Related Questions