Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Template that adds a given attribute and value

Question posted by: Hvid Hat (Guest) on June 27th, 2008 07:07 PM
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? :-)

Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:07 PM
#2

Re: Template that adds a given attribute and value
Hvid Hat wrote:
Quote:
Originally Posted by
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>
--

Martin Honnen
http://JavaScript.FAQTs.com/

Hvid Hat's Avatar
Hvid Hat
Guest
n/a Posts
June 27th, 2008
07:07 PM
#3

Re: Template that adds a given attribute and value
On 07-06-2008 15:05:54, Martin Honnen wrote:
Quote:
Originally Posted by
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.

 
Not the answer you were looking for? Post your question . . .
182,081 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors