Is it possible to perform conditional totalling in xsl?
I have the following xml sample:
<?xml version="1.0" encoding="utf-8"?>
<export>
<stats set="1">
<columns>
<column id="0">
<sum>100</sum>
</column>
<column id="1">
<sum>102</sum>
</column>
<column id="2">
<sum>12</sum>
</column>
</columns>
</stats>
<stats set="2">
<columns>
<column id="0">
<sum>100</sum>
</column>
<column id="1">
<sum>101</sum>
</column>
<column id="2">
<sum>19</sum>
</column>
</columns>
</stats>
</export>
Is it possible to compute the total of all columns in each stat set where they are not equal to one another? So it would output the following:
Set 1 Set 2 Diff(Set 1 - Set 2)
Total (Diff) 114 120 -6
column 2 102 101 1
column 3 12 19 -7
So in the output column 1 would be omitted as the sum in the two stat sets is the same.
I can get my xsl to output the columns that are different but unsure how to total these up and put in the total row.
Many thanks,
Andez
This transformation (64 lines):
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
The key named
kColByPosAndValis used to select all columns that have a given position (among allcolumnsiblings) and a given value for theirsumchild-element.The key named
kIdByValis used in Muenchian grouping to find all different values for theidattribute.The two totals are calculated summing only those columns, whose
kColByPosAndValkey selects only onecolumnelement (if it selects twocolumnelements, they both are at the same position and have the samesum).The rest should be easy to understand.