Search code examples
xsltsumxslt-2.0xslt-grouping

xslt 2.0 for-each-group not returning all elements


I am trying to use for-each-group to summarize values, but it seems like for-each-group is not returning all elements. I am trying to group by and sum based on the unique combinations of PRIORYR, CYCLE and CODE. My current code is only getting the first TI_DET element. Here is the input xml:

<TI_DET id="2">
    <PRIORYR>2014</PRIORYR>
    <CYCLE>1</CYCLE>
    <CODE>TAX</CODE>
    <AMOUNT>44.35</AMOUNT>
</TI_DET>
<TI_DET id="3">
    <PRIORYR>2014</PRIORYR>
    <CYCLE>1</CYCLE>
    <CODE>TAX</CODE>
    <AMOUNT>59.52</AMOUNT>
</TI_DET>
<TI_DET id="4">
    <PRIORYR>2014</PRIORYR>
    <CYCLE>1</CYCLE>
    <CODE>TAX</CODE>
    <AMOUNT>20.77</AMOUNT>
</TI_DET>
<TI_DET id="5">
    <PRIORYR>2014</PRIORYR>
    <CYCLE>1</CYCLE>
    <CODE>TAX</CODE>
    <AMOUNT>0.74</AMOUNT>
</TI_DET>
<TI_DET id="6">
    <PRIORYR>2014</PRIORYR>
    <CYCLE>1</CYCLE>
    <CODE>TAX</CODE>
    <AMOUNT>1.48</AMOUNT>
</TI_DET>
<TI_DET id="7">
    <PRIORYR>2014</PRIORYR>
    <CYCLE>1</CYCLE>
    <CODE>TAX</CODE>
    <AMOUNT>1.31</AMOUNT>
</TI_DET>

And here is my current code:

    <xsl:for-each-group select="/ns1:GetChargesResponse/PROPERTY/TI_DETS/TI_DET" group-by="PRIORYR">
        <xsl:sort select="PRIORYR" data-type="text" order="ascending"/>
        <xsl:for-each-group select="current-group()" group-by="CYCLE">
            <xsl:sort select="CYCLE" data-type="text" order="ascending"/>
            <xsl:for-each-group select="current-group()" group-by="CODE">
                <xsl:sort select="CODE" data-type="text" order="ascending"/>
                <xsl:for-each-group select="current-group()" group-by="AUTH">
                <client:Charges>
                  <client:Description>
                    <xsl:value-of select="CODE"/>
                  </client:Description>
                  <client:TaxYear>
                    <xsl:value-of select="PRIORYR"/>
                  </client:TaxYear>
                  <client:Amount>
                    <xsl:value-of select="sum(AMOUNT)"/>
                  </client:Amount>
                  <client:Notes>
                    <xsl:value-of select="concat(PRIORYR,';',ROLLTYPE,';',CYCLE,';',CODE)"/>
                  </client:Notes>
                  <client:RedemptionCertificateNumber>
                    <xsl:value-of select="/ns1:GetChargesResponse/ns1:GetChargesResponse"/>
                  </client:RedemptionCertificateNumber>
                  <client:PayType>
                    <xsl:text disable-output-escaping="no">FUL</xsl:text>
                  </client:PayType>
                  <client:TaxCycle>
                    <xsl:value-of select="CYCLE"/>
                  </client:TaxCycle>
                  </client:Charges>
                  </xsl:for-each-group>
            </xsl:for-each-group>
        </xsl:for-each-group>
    </xsl:for-each-group>

Thoughts?


Solution

  • For one thing, you have group-by="AUTH"- but there is no AUTH element to group by.

    For another, if you want to sum the current group, you need to change:

    <xsl:value-of select="sum(AMOUNT)"/>
    

    to:

    <xsl:value-of select="sum(current-group()/AMOUNT)"/>
    

    Otherwise you'll be only getting the value of the first item in the current group.