How to dynamically name an attribute?
Asked Answered
F

3

6

I'm looking to get a custom attribute for anchor tag from xsl.

Is it possible to get the name of the attribute dynamically from xml?

Here is what I tried :

<xsl:attribute name="<xsl:value-of select="id"/>">
   <xsl:value-of select="value"/>
</xsl:attribute>
Fard answered 20/2, 2015 at 14:37 Comment(1)
Your question is not clear. Why don't you post an example of the input, and the expected output ?Disparagement
F
9

Yes, it is possible. You can pass a variable as name value.

<xsl:variable name="attributeName" select="id"/>
<xsl:attribute name="{$attributeName}">
    <xsl:value-of select="value"/>
</xsl:attribute>
Finback answered 20/2, 2015 at 14:41 Comment(0)
A
6

You can simpify the solution from @Savard to

<xsl:attribute name="{id}">
    <xsl:value-of select="value"/>
</xsl:attribute>

or if you are using XSLT 2.0, to

<xsl:attribute name="{id}" select="value"/>
Arguseyed answered 20/2, 2015 at 15:51 Comment(1)
<xsl:template name="gen_attr_if_positive_int"> <xsl:param name="attr_name"/> <xsl:param name="attr_value"/> <xsl:choose> <xsl:when test="$attr_value &gt; 0"> <xsl:attribute name="{attr_name}"> <xsl:value-of select="$attr_value"/> </xsl:attribute> </xsl:when> </xsl:choose> </xsl:template> --------- gives me XTDE0850 Invalid attribute name: ----- so does {$attr_name} or just $attr_nameUme
P
0

Note, you can also mix-and-match dynamic and literal parts to a name:

<xsl:attribute name="{name}_example">Test</xsl:attribute>
Pedlar answered 21/6, 2022 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.