XSLT: add an attribute

J

Jeff Higgins

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
 
J

Joe Kesselman

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
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top