xslt 2.0 for-each-group不返回所有元素

时间:2016-09-26 21:55:11

标签: xslt sum xslt-2.0 xslt-grouping

我正在尝试使用for-each-groups来汇总值,但似乎for-each-group并没有返回所有元素。我试图根据PRIORYR,CYCLE和CODE的独特组合进行分组和求和。我当前的代码只获得第一个TI_DET元素。这是输入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>

这是我目前的代码:

    <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>

思想?

1 个答案:

答案 0 :(得分:1)

首先,您有group-by="AUTH" - 但没有AUTH元素可以分组。

另一方面,如果要对当前组求和,则需要更改:

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

为:

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

否则您只会获得当前组中第一项的值。