XSLT: add an attribute

Jeff Higgins

Usenet Archive
Joined
Sep 27, 2005
Messages
1,207
Reaction score
0
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
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
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.
 

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
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top