Template that adds a given attribute and value

H

Hvid Hat

Hi

I've been messing around with adding attributes to certain nodes. I've looked
at FAQ at http://www.dpawson.co.uk/ without finding what I'm looking for.

Is it possible to have a template that is called with a node, an attribute
name and an attribute value. The template would add the the given attribute
name with the attribute value to the given note. Is this possible with XSLT -
and if so, where can I get it? :)
 
M

Martin Honnen

Hvid said:
I've been messing around with adding attributes to certain nodes. I've looked
at FAQ at http://www.dpawson.co.uk/ without finding what I'm looking for.

Is it possible to have a template that is called with a node, an attribute
name and an attribute value. The template would add the the given attribute
name with the attribute value to the given note. Is this possible with XSLT -
and if so, where can I get it? :)

You have already posted much of the solution:
<xsl:template name="add-attribute">
<xsl:param name="el"/>
<xsl:param name="att-name"/>
<xsl:param name="att-value"/>
<xsl:for-each select="$el">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="{$att-name}">
<xsl:value-of select="$att-value"/>
</xsl:attribute>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>

Call as

<xsl:call-template name="add-attribute">
<xsl:with-param name="el" select="foo"/>
<xsl:with-param name="att-name" select="'bar'"/>
<xsl:with-param name="att-value" select="'baz'"/>
</xsl:call-template>
 
H

Hvid Hat

You have already posted much of the solution:
<xsl:template name="add-attribute">
<xsl:param name="el"/>
<xsl:param name="att-name"/>
<xsl:param name="att-value"/>
<xsl:for-each select="$el">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="{$att-name}">
<xsl:value-of select="$att-value"/>
</xsl:attribute>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>

Call as

<xsl:call-template name="add-attribute">
<xsl:with-param name="el" select="foo"/>
<xsl:with-param name="att-name" select="'bar'"/>
<xsl:with-param name="att-value" select="'baz'"/>
</xsl:call-template>

Perfect! Thanks, Martin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top