Christophe Marchand
Christophe Marchand

Reputation: 31

How to eval an XSL constructed XPath expression

Humm...

<xsl:value-of select="$document/@*[name() eq $attrName]"/>

seems to be the solution... Regards, Christophe

I have a variable that contains the name of an attribute to query. How can I wrote such an XPath expression ? Here is an example of what I would like to do

<xsl:variable name="attrName" select="$config//conf:document[@id=$docId]/@archive-ventil-attr"/>
<xsl:value-of select="$document/@{$attrName}"/>

I use XSLT 2.0

Thanks a lot in advance, Christophe

Upvotes: 3

Views: 3332

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167506

With a complete XPath expression you would need an evaluate function which is in XSLT 1.0 and 2.0 only available as an extension, see saxon:evaluate. Also, in XSLT 3.0 (https://www.w3.org/TR/xslt-30/#element-evaluate) (since Saxon 9.8 and later PE and EE, Altova XML 2017 R3 and later) there is an instruction element named xsl:evaluate.

As long as you simply have a string with a local name it should suffice to use <xsl:value-of select="$elements/@*[local-name() = $attrName]"/>. This assumes $elements is a node-set (XPath 1.0) or sequence (XPath 2.0) of element nodes.

Upvotes: 3

Related Questions