XSLT: add an attribute

Discussion in 'XML' started by Jeff Higgins, May 13, 2006.

  1. Jeff Higgins

    Jeff Higgins Guest

    Hi,

    How can I add an attribute to selected elements?

    For each section element in the source document,
    I would like to add a name attribute where the attribute is equal to:
    <xsl:number level="multiple" count="section" format="1_1"/>

    For source document:

    <src>
    <section>
    <section>
    <section/>
    <section/>
    <section/>
    </section>
    </section>
    </src>

    The result document would be:

    <src>
    <section name="1">
    <section name="1_1">
    <section name="1_1_1"/>
    <section name="1_1_2"/>
    <section name="1_1_3"/>
    </section>
    </section>
    </src>

    Thanks,
    Jeff Higgins
     
    Jeff Higgins, May 13, 2006
    #1
    1. Advertising

  2. Start with the identity stylesheet, then add something like:

    <xsl:template match="section">
    <xsl:copy>
    <xsl:attribute name="name">
    <xsl:number level="multiple" count="section"
    format="1_1"/></xsl:attribute>
    <xsl:apply-templates select="@*|*"/>
    </xsl:copy>
    </xsl:template>


    In other words: Copy the <section>, add the attribute, then recursively
    process the other attributes and children.
     
    Joe Kesselman, May 13, 2006
    #2
    1. Advertising

  3. Jeff Higgins

    Jeff Higgins Guest

    Thanks Joe.
     
    Jeff Higgins, May 13, 2006
    #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. Stylus Studio
    Replies:
    0
    Views:
    714
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,700
    Dimitre Novatchev
    Sep 25, 2003
  3. Bostonasian
    Replies:
    1
    Views:
    826
    Joris Gillis
    Sep 18, 2005
  4. bprimuslevy
    Replies:
    1
    Views:
    454
    Soren Kuula
    Aug 23, 2006
  5. Stefan_W
    Replies:
    1
    Views:
    8,295
    Stefan_W
    Nov 5, 2008
Loading...

Share This Page