views:

19

answers:

2

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"&gt;

 <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?

+1  A: 

To answer your question: you wrap the call to the template(s) in a variable declaration.

<xsl:variable name="result">
  <!-- call or apply templates here -->
  <xsl:call-template name="my-template"/>
</xsl:variable>
<!-- use the defined value: -->
<xsl:copy-of select="$result"/>

However there is, in your case a better way to get total number of items arbitrarily deep in the nested structure: use XPath wildcards:

<xsl:value-of select="count(.//item)"/>

You can use this like so:

<xsl:template match="assessment|section">
   <!-- output section subtotal -->
   <!-- print name : number-of-items, adapt to get the proper section name -->
   <xsl:value-of select="concat(@name, ': ', count(.//item))"/>
   <!-- recurse: sub sections will get their subtotals listed, too -->
   <xsl:apply-templates select="*"/>
</xsl:template>
A: 

Here is an XSLT 2.0 solution

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my"
 >
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

    <xsl:template match="assessment|section">
      <xsl:value-of select="concat(@name, ': ', my:sumItems(.), ' items&#xA;')"/>
      <xsl:apply-templates/>
    </xsl:template>

    <xsl:function name="my:sumItems" as="xs:integer*">
      <xsl:param name="pNode" as="element()"/>

      <xsl:sequence select="count($pNode/item) + sum($pNode/section/my:sumItems(.))"/>
    </xsl:function>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<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>

the wanted, correct result is produced:

Assessment: 12 items
Section1: 5 items
Section2: 7 items
Section2-2: 4 items

While in this particular problem one could use a straightforward XPath expression (count(.//item)), this solution demonstrates a generall and useful pattern for problems that don't allow such immediate calculation.

Dimitre Novatchev