Template that adds a given attribute and value

Discussion in 'XML' started by Hvid Hat, Jun 7, 2008.

  1. Hvid Hat

    Hvid Hat Guest

    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? :)
     
    Hvid Hat, Jun 7, 2008
    #1
    1. Advertising

  2. 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">
    <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/
     
    Martin Honnen, Jun 7, 2008
    #2
    1. Advertising

  3. Hvid Hat

    Hvid Hat Guest

    On 07-06-2008 15:05:54, Martin Honnen wrote:

    > 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.
     
    Hvid Hat, Jun 8, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ddfsdf
    Replies:
    0
    Views:
    360
    ddfsdf
    Oct 11, 2005
  2. Alan Silver
    Replies:
    2
    Views:
    558
    jakewan
    May 15, 2006
  3. Lord0
    Replies:
    1
    Views:
    573
    Thomas Weidenfeller
    Apr 19, 2006
  4. Replies:
    1
    Views:
    4,293
    Joe Kesselman
    Aug 2, 2006
  5. Replies:
    8
    Views:
    523
Loading...

Share This Page