xmlxsltattributesexslt

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?


Solution

  • 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].