Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Template that adds a given attribute and value

Reply
Thread Tools

Template that adds a given attribute and value

 
 
Hvid Hat
Guest
Posts: n/a
 
      06-07-2008
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?

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      06-07-2008
Hvid Hat wrote:

> 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">
<xslaram name="el"/>
<xslaram name="att-name"/>
<xslaram 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/
 
Reply With Quote
 
 
 
 
Hvid Hat
Guest
Posts: n/a
 
      06-08-2008
On 07-06-2008 15:05:54, Martin Honnen wrote:

> You have already posted much of the solution:
> <xsl:template name="add-attribute">
> <xslaram name="el"/>
> <xslaram name="att-name"/>
> <xslaram 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Change the value of an attribute according to the value of another attribute patrizio.trinchini@googlemail.com XML 8 08-22-2006 02:53 PM
wrapping long attribute value (line-continuation for attribute value) lophiomys@gmx.at XML 1 08-02-2006 01:18 PM
ASP.NET adds invalid attribute to the form!! Alan Silver ASP .Net 2 05-15-2006 10:34 PM
c# webapp template - how to custom a template so it automatically adds files within directories? ddfsdf ASP .Net 0 10-11-2005 02:47 PM



Advertisments