I have XML like this:
<assessment name="Assessment">
<section name="Section1">
<item name="Item1-1"/>
<item name="Item1-2"/>
<item name="Item1-3"/>
<item name="Item1-4"/>
<item name="Item1-5"/>
</section>
<section name="Section2">
<item name="Item2-1"/>
<item name="Item2-2"/>
<item name="Item2-3"/>
<section name="Section2-2">
<item name="Item2-2-1"/>
<item name="Item2-2-2"/>
<item name="Item2-2-3"/>
<item name="Item2-2-4"/>
</section>
</section>
</assessment>
As you can see, an assessment can contain sections. A section can contain sections and/or items.
I want to use XSLT to recursively count the number of items in assessments and sections. So, my transformation should output something like this:
Assessment: 12 items
Section1: 5 items
Section2: 7 items
Section2-2: 4 items
I have a start with this recursive XSLT:
<stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<output method="text"/>
<template match="/assessment">
<xsl:for-each select="section">
<xsl:call-template name="sectionCount">
</xsl:call-template>
</xsl:for-each>
</template>
<template name="sectionCount">
<xsl:variable name="items" select="item"/>
<xsl:for-each select="section">
<xsl:call-template name="sectionCount">
</xsl:call-template>
</xsl:for-each>
<xsl:value-of select="count($items)"/>
</template>
</stylesheet>
What I can't figure out how to do is pass the values back up and add them. How is this done, exactly?