Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically name an attribute?

Tags:

xslt

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>
like image 367
user2628187 Avatar asked Feb 20 '15 14:02

user2628187


2 Answers

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>
like image 194
Jean-François Savard Avatar answered Oct 05 '22 14:10

Jean-François Savard


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"/>
like image 38
Michael Kay Avatar answered Oct 05 '22 13:10

Michael Kay