user1932794
user1932794

Reputation: 25

Nested grouping based on a condition

I am learning on xslt group-by. I have a sample xml as below

<?xml version="1.0" encoding="UTF-8"?>
<h1>
    <a1>abcd</a1>
    <a2>efgh</a2>
    <h2>
        <b1>IV-3</b1>
        <b2>20.00</b2>
        <h3>
            <c1>VCH</c1>
            <c2>1001</c2>
            <c3>100.00</c3>
        </h3>
    </h2>
    <h2>
        <b1>IV-3</b1>
        <b2>50.00</b2>      
    </h2>   
    <h2>
        <b1>IV-3</b1>
        <b2>10.00</b2>
        <h3>
            <c1>VCH</c1>
            <c2>1001</c2>
            <c3>300.00</c3>
        </h3>
    </h2>
    <h2>
        <b1>IV-3</b1>
        <b2>30.00</b2>      
    </h2>
</h1>

I need to group-by with the b1 value first and sum up b2 values and if h3 node is present , need to group-by c2 and sum up c3 values of that group. My xslt is as below.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="h1">
        <t1>
            <d1>
                <xsl:value-of select="a1"/>
            </d1>
            <d2>
                <xsl:value-of select="a2"/>
            </d2>
            <xsl:for-each-group select="h2" group-by="b1">
                <xsl:if test="current-group()/count(h3) > 0 ">
                    <xsl:for-each-group select="current-group()/h3" group-by="c2">
                        <t2>
                            <e1>
                                <xsl:value-of select="../b1"/>
                            </e1>
                            <e2>
                                <xsl:value-of select="format-number(sum(current-group()/../b2),'#.00')"/>
                            </e2>
                            <e3>
                            <xsl:value-of select="format-number(sum(current-group()/c3),'#.00')"/>
                            </e3>
                        </t2>
                    </xsl:for-each-group>
                </xsl:if>
                <xsl:if test="current-group()/count(h3) = 0 ">                  
                        <t2>
                            <e1>
                                <xsl:value-of select="b1"/>
                            </e1>
                            <e2>
                                <xsl:value-of select="format-number(sum(current-group()/b2),'#.00')"/>
                            </e2>
                            <e3>
                            <xsl:value-of select="11111"/>
                            </e3>
                        </t2>
                </xsl:if>
            </xsl:for-each-group>
        </t1>
    </xsl:template>
</xsl:stylesheet>

The aggrigate value I am getting for e2 is not correct, if h3 is not present. The expected result is

<?xml version="1.0" encoding="UTF-8"?>
<t1 xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <d1>abcd</d1>
    <d2>efgh</d2>
    <t2>
        <e1>IV-3</e1>
        <e2>30.00</e2>
        <e3>400.00</e3>
    </t2>
    <t2>
        <e1>IV-3</e1>
        <e2>80.00</e2>
        <e3>11111</e3>
    </t2>
</t1>

Thanks for the help!

Upvotes: 1

Views: 1127

Answers (1)

Tomalak
Tomalak

Reputation: 338376

You want to group on two values at once, so you must change your group-by selector.

From

<xsl:for-each-group select="h2" group-by="b1">

To

<xsl:for-each-group select="h2" group-by="concat(b1, '|', boolean(h3))">

Apart from that your code works.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="h1">
    <t1>
      <d1>
        <xsl:value-of select="a1" />
      </d1>
      <d2>
        <xsl:value-of select="a2" />
      </d2>
      <xsl:for-each-group select="h2" group-by="concat(b1, '|', boolean(h3))">
        <xsl:if test="current-group()/h3">
          <xsl:for-each-group select="current-group()/h3" group-by="c2">
            <t2>
              <e1>
                <xsl:value-of select="../b1" />
              </e1>
              <e2>
                <xsl:value-of select="format-number(sum(current-group()/../b2), '#.00')" />
              </e2>
              <e3>
                <xsl:value-of select="format-number(sum(current-group()/c3), '#.00')" />
              </e3>
            </t2>
          </xsl:for-each-group>
        </xsl:if>
        <xsl:if test="not(current-group()/h3)">
          <t2>
            <e1>
              <xsl:value-of select="b1" />
            </e1>
            <e2>
              <xsl:value-of select="format-number(sum(current-group()/b2), '#.00')" />
            </e2>
            <e3>
              <xsl:value-of select="11111" />
            </e3>
          </t2>
        </xsl:if>
      </xsl:for-each-group>
    </t1>
  </xsl:template>
</xsl:stylesheet>

This stylesheet produces the wanted output

<t1 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
   <d1>abcd</d1>
   <d2>efgh</d2>
   <t2>
      <e1>IV-3</e1>
      <e2>30.00</e2>
      <e3>400.00</e3>
   </t2>
   <t2>
      <e1>IV-3</e1>
      <e2>80.00</e2>
      <e3>11111</e3>
   </t2>
</t1>

Notes

  • boolean(h3) emits the strings 'true' or 'false' depending on the existence of <h3> elements
  • in the same manner, your <xsl:if> tests can be simplified to current-group()/h3 and not(current-group()/h3), respectively (the empty node set evaluates to false)
  • you should declare exclude-result-prefixes="xs fn" to remove the unnecessary namespace declarations from the output document.

Upvotes: 0

Related Questions