Cees Mandjes
Cees Mandjes

Reputation: 195

XSLT Get attribute by string

I'm wondering if I can get a attr value by a string query on a XML element. Example:

<xsl:variable name="astr">
color
</xsl:variable>

<xsl:value-of select="$treeItem/@$astr"></xsl:value-of> 

Which means:

$treeItem/@$astr   results in ---> $treeItem/@color

Is this possible?

Upvotes: 0

Views: 446

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167506

The way you have set up your variable it is not even a string but a result tree fragment or a temporary tree.

To have a string you need e.g. <xsl:variable name="astr" select="'color'"/>.

As for selecting an attribute by the name you have in your variable, use $treeItem/@*[local-name() = $astr].

Upvotes: 2

Related Questions