Uncategorized
Uncategorized Contents
Summary This snippet demonstrates how to group elements and their sub-elements calculating totals and sub-totals. The example has two levels of nesting only, however the demonstrated technique can be generalised for multiple levels.
The following question was asked on the microsoft.public.xsl newsgroup:
How to transform weights.xml file (see the xml code), generated from recordset, intototals.xml?<code/> and <weight/> are already pre-sorted.
The result (totals.xml) should look like this:
<?xml version=1.0?> <total> <code c=2A1669> <subtotal>3</subtotal> <nr n=712374
<?xml version=1.0?> <total> <code c=2A1669
<?xml version=1.0?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output omit-xml-declaration=yes indent=yes/> <xsl:key name=kCode match=code use=./> <xsl:key name=kNr match=nr use=../code/> <xsl:key name=kCodeNr match=nr use=concat(../code, '|', .)/> <xsl:template match=node()/> <xsl:template match=/> <total> <xsl:apply-templates select=/xml/row/code/> <xsl:text>
</xsl:text> </total> </xsl:template> <xsl:template match=code[count(.|key('kCode',.)[1])=1]> <xsl:text>
</xsl:text> <code c={.}> <subtotal><xsl:value-of select=sum(key('kCode',.)/../weight)/></subtotal> <xsl:for-each select=key('kNr', .)[count(. | key('kCodeNr', concat(../code, '|', .))[1])=1]> <nr n={.}><xsl:value-of select=sum(key('kCodeNr', concat(../code, '|', .))/../weight)/></nr> <xsl:text>
</xsl:text> </xsl:for-each> </code> </xsl:template> </xsl:stylesheet>
<?xml version=1.0?> <xsl:stylesheet version=1.0
1. The Muenchian technique is used to process all different <code> elements (the first of each set of <code> elements having the same value).This is specified in the match attribute of the template:
<xsl:template match=code[count(.|key('kCode',.)[1])=1]>
2. The total is just a summation of the weight siblings of all <code> elements with the same value:
<subtotal><xsl:value-of select=sum(key('kCode',.)/../weight)/></subtotal>
3. All different <nr> children of a given <code> element are specified again using the Muenchian method (only the first out of each group of <nr>-s with the same value). To ensure that only the <nr> children of the current <code> element will be specified, a different xsl:key, named kCodeNr is used. This key yields all <code><nr> combinations, where the <nr>-s must be children of the <code>:
<xsl:for-each select=key('kNr', .)[count(. | key('kCodeNr', concat(../code, '|', .))[1])=1]>
4. The sub-total for all <nr> children with the same value is just a summation of the weight siblings of all the <nr> siblings of the current <nr> having the same value: <nr n={.}><xsl:value-of select=sum(key('kCodeNr', concat(../code, '|', .))/../weight)/></nr>
2A1669
2A1747
2A1840
DAPR21
3 2 1
19 9 10
32 4.5 6 15 6.5
26.5 10.5 5 3.5 7.5
Partners
Dream.In.Code dotNet Slackers dotNet Spider Your HTML Source VisualBuilder.com DevGuru Planet Source Code ZVON.ORG Web Design ASPAlliance XML Pitstop Scripts
The Spot 4 SAP Bitshop Web Hosting