Sorokin Evgeny
Sorokin Evgeny

Reputation: 98

XSL use variable to get attribute

I have a code to output options list:

<xsl:template match="*" mode="option">
    <xsl:param name="value" select="'value'"/>
    <option>
        <xsl:attribute name="value" select="./@($value)"/>
        <xsl:value-of select="./@name"/>
    </option>
</xsl:template>

The question is how can i get an attribute value which name is in variable $value? What should i use instead of not working construction: ./@($value).

Upvotes: 1

Views: 339

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167426

If you want to use a parameter or variable value then $value is all you need, i.e. <xsl:attribute name="value" select="$value"/> to create an attribute with name value and with the value of the parameter which is also value. If you want to read out the value of an attribute named like your parameter then you would use <xsl:attribute name="value" select="@*[local-name() eq $value]"/>.

You could also consider an attribute value template for brevity:

<option value="{$value}"><xsl:value-of select="@name"/></option>

respectively

<option value="{@*[local-name() eq $value]}"><xsl:value-of select="@name"/></option>

Upvotes: 1

Related Questions