xsl-list
[Top] [All Lists]

Re: [xsl] Help with 3-level deep XML

2007-05-16 05:34:46
Bhaskar wrote:
Hi,

I need help with a transformation I am trying to do. The XML is:


26
Cut n paste error? This is not XML and I don't see any levels in it.


I am basically trying to get the sum of third-level deep elements categorized 
by their names. That is, the output will be:
ccc1   40 (26+14)
ccc2   6 (-36+42)

you can get the name of an element using name() or local-name().
you can find the depth of an element using count(ancestor-or-self::*). So, suppose you have any input (provided it is XML) and it has some hierarchy of nodes, I think you can do something like this, which will :

<xsl:template match="/" name="main">
<xsl:for-each-group select="$data//*[count(ancestor-or-self::*) = 3]" group-by="local-name()" >
       <xsl:value-of select="local-name(), '   ',
           sum(current-group()), '(',
           (current-group()/(string(), '+'))[position() != last()],
           ')&#xA;'"  />
   </xsl:for-each-group>
</xsl:template>

where the core of the for-each is actually sum(current-group()). The rest is to make it nice strings. If you use the following input:

<l1>
   <l2>
       <l3a>234</l3a>
       <l3b>2755</l3b>
   </l2>
   <l2>
       <l3c>32</l3c>
       <l3b>76</l3b>
       <l3b>5345</l3b>
       <l3b>233</l3b>
       <l3c>43</l3c>
   </l2>
   <l2>
       <l3a>112</l3a>
       <l3a>987</l3a>
   </l2>
</l1>


it will output this:
l3a     1333 ( 234 + 112 + 987 )
l3b     8409 ( 2755 + 76 + 5345 + 233 )
l3c     75 ( 32 + 43 )

HTH,
Cheers,
-- Abel Braaksma


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>