RE: [xsl] Totals for conditional sums ?maybe?

Subject: RE: [xsl] Totals for conditional sums ?maybe?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 3 Dec 2005 17:09:55 -0000
> What I am trying to do is total the ORDD_TLNA for the base 
> years when 
> //AwardFullData/AwardItemizedLine[count(./periodLinePeriodNumb
> er)&lt;1] and then later total ORDD_TLNA for the option years 
> when 
> //AwardFullData/AwardItemizedLine[count(./periodLinePeriodNumb
> er)&gt;0]">
> 
> I have tried so many iterations I have forgotten them all but 
> I think the closest I have gotten is this:
> 
> <xsl:variable name="TEST" 
> select="//AwardFullData/AwardItemizedLine[count(./periodLinePe
> riodNumber)&lt;1]"/>
> <xsl:variable name="ADDME" 
> select="//AwardFullData/AwardItemizedLine/transactionAmount"/>
> ...

For some reason you've made this much more complicated than it is. Just
select the nodes you want to total and apply the sum() function:

 
sum(//AwardFullData/AwardItemizedLine[count(./periodLinePeriodNumber)&lt;1]/
transactionAmount"/>

I'm assuming here that transactionAmount does actually hold a numeric value.
You haven't shown your source document, but there's a possible hint later in
your question that suggests trasactionAmount might hold a currency sign. If
that's the case then in 2.0 you can do

sum(//AwardFullData/AwardItemizedLine[count(./periodLinePeriodNumber)&lt;1]/
transactionAmount/
       number(translate(.,'$',''))"/>
> 
> <xsl:value-of select="sum($ADDME[$TEST])"/>

There are several reasons this is wrong. Firstly, any expression of the form
$ADDME[XXX] is going to select a subset of the nodes in $ADDME. These are
AwardItemizedLine nodes, whereas you want to sum transactionAmount nodes.
Secondly, $TEST is taken as true if
//AwardFullData/AwardItemizedLine/transactionAmount selects any nodes, and
as false if it doesn't; the value doesn't depend in any way on the
AwardAtomizedLine node in question.
> 
> 
> Not even sure if the above was correct I tried to strip the 
> formatting of the data which includes $ and , (I have no 
> control over the data source).

At this point I think we need to see what the data looks like.

A general comment: you're trying to do this by trial and error, which isn't
a very good way of learning the language. The stylesheets you're working
with aren't very well written, so learning from them isn't a good idea
either. Take some time to read a good XSLT book.

Michael Kay
http://www.saxonica.com/

Current Thread