0

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

2
  • What happens if you simply remove the single quotes around DefaultTag? Otherwise, please post a complete XSLT stylesheet so that people can test themselves. Commented Feb 23, 2014 at 17:44
  • How are you setting the parameter? Commented Feb 23, 2014 at 17:46

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! That was it. I knew there had to be a way ... Thanks!
@DennisButtery Please accept this answer (mark the tick on the left so that it appears in green). This is common practice on Stackoverflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.