Using XSLT to add attribute to element that is a counter for that element (within a particular xpath)

Using the following sample data I would like to add an attribute to all the chapter elements that indicates their 'number' - I only want to do this for chapter elements within topgroup.
Chapter elements may be present elsewhere but I am not interested in them.

Similarly with the section elements but only when within a bottomgroup element. Section elements could be present elsewhere but again, I'm not interested in them as far adding this attribute.

Any help gratefully received - thanks!

<wrapper>
	...
	<topgroup>
 		<chapter attr1="XX" attr2="YY">
 			..
 		</chapter>
 		<chapter attr1="XX" attr2="YY">
 			..
 		</chapter>
 		<chapter attr1="XX" attr2="YY">
 			..
 		</chapter>
	</topgroup>
	<bottomgroup>
		<section attr1="ZZ">
			..
		</section>
		<section attr1="ZZ">
			..
		</section>
	</bottomgroup>
</wrapper>

Open in new window

                 


I would like to end up with the following:
<wrapper>
	...
	<topgroup>
 		<chapter attr1="XX" attr2="YY" chapternum="1">
 			..
 		</chapter>
 		<chapter attr1="XX" attr2="YY" chapternum="2">
 			..
 		</chapter>
 		<chapter attr1="XX" attr2="YY" chapternum="3">
 			..
 		</chapter>
	</topgroup>
	<bottomgroup>
		<section attr1="ZZ" sectionnum="1">
			..
		</section>
		<section attr1="ZZ" sectionnum="2">
			..
		</section>
	</bottomgroup>
</wrapper>	

Open in new window

LVL 1
LetterpartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="topgroup/chapter">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="chapternum">
                <xsl:value-of select="count(preceding-sibling::chapter) + 1"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Gertone (Geert Bormans)Information ArchitectCommented:
I used "count(preceding::sibling::chapter) + 1" instead op "position()"
to allow for potential other elements intermixed
The above would not work if you would have multiple topgroup
(unless you want restarting the numbering)... but there are different solutions for that if so
LetterpartAuthor Commented:
Thanks Geert.

I'm sorry but it looks like I left out a bit of my structure in the example.
chapter can appear in topgroup or within an innertopgroup
i.e. /topgroup/chapter   or   /topgroup/innertopgroup/chapter

So, a better example is:
<wrapper>
	...
	<topgroup>
 		<chapter attr1="XX" attr2="YY">
 			..
 		</chapter>
 		<innertopgroup>
		 		<chapter attr1="XX" attr2="YY">
		 			..
		 		</chapter>
 		</innertopgroup>
 		<innertopgroup>
		 		<chapter attr1="XX" attr2="YY">
		 			..
		 		</chapter>
 		<innertopgroup>
	</topgroup>
	<bottomgroup>
		<section attr1="ZZ">
			..
		</section>
		<section attr1="ZZ">
			..
		</section>
	</bottomgroup>
</wrapper>			

Open in new window



I would like to end up with the following:

<wrapper>
	...
	<topgroup>
 		<chapter attr1="XX" attr2="YY" chapternum="1">
 			..
 		</chapter>
 		<innertopgroup>
		 		<chapter attr1="XX" attr2="YY" chapternum="2">
		 			..
		 		</chapter>
 		</innertopgroup>
 		<innertopgroup>
	 		<chapter attr1="XX" attr2="YY" chapternum="3">
	 			..
	 		</chapter>
 		</innertopgroup>
	</topgroup>
	<bottomgroup>
		<section attr1="ZZ" sectionnum="1">
			..
		</section>
		<section attr1="ZZ" sectionnum="2">
			..
		</section>
	</bottomgroup>
</wrapper>	

Open in new window

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Gertone (Geert Bormans)Information ArchitectCommented:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="topgroup/chapter | topgroup/innertopgroup/chapter">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="chapternum">
                <xsl:value-of select="count(preceding::chapter[parent::topgroup | parent::innertopgroup/parent::topgroup ]) + 1"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LetterpartAuthor Commented:
excellent - thanks very much Geert.
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.