RE: [xsl] Continuously add to a parameter at each call

Subject: RE: [xsl] Continuously add to a parameter at each call
From: "Haarman, Michael" <mhaarman@xxxxxxxxx>
Date: Wed, 9 Nov 2005 17:14:21 -0600
Emil,

> <Tasks>
> <Task><Name>Hardware</Name><OutlineLevel>1</OutlineLevel><WBS>
> 1</WBS></T
> ask>
> <Task><Name>Bringup</Name><WBS>1.1</WBS><Cost>12</Cost></Task>
> <Task><Name>Testing</Name><WBS>1.2</WBS><Cost>22</Cost></Task>
> <Task><Name>Software</Name><OutlineLevel>1</OutlineLevel><WBS>
> 2</WBS></T
> ask>
> <Task><Name>Development</Name><WBS>2.1</WBS><Cost>1</Cost></Task>
> <Task><Name>Test</Name><WBS>2.2</WBS><Cost>3</Cost></Task>
> </Task>

For a document like the snip above, something like the following gets very
close to what you want.  Things to note:

a) the top-level xsl:key instruction is standard means of grouping,
b) the *use* attr of key takes a XPath expression evaluating to a string
c) sum takes a node-set, which is the return type of key() function


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

  <xsl:key name="tasks" match="*/Task" use="substring(WBS, 1, 1)"/>

  <xsl:template match="/">
    <totals>
      <xsl:for-each select="*/Task[OutlineLevel = '1']">
        <p>
          <xsl:value-of select="Name"/> total: 
          <xsl:value-of select="sum(key('tasks', WBS)/Cost)"/>
        </p>
      </xsl:for-each>
    </totals>
  </xsl:template>

</xsl:stylesheet>


A plain English paraphrase of the select expression of the for-each is
obvious.  The select expression that does the work means:

Current Thread