在分组父元素XSLT中对子元素进行分组

时间:2014-04-02 12:09:01

标签: xml xslt foreach grouping

我正在使用for-each-group组合XML元素,而我遇到的问题是这些XML元素具有子元素,这些子元素具有自己的子元素。 我也想结合儿童元素。

我的输入XML如下所示:

<Root>
   <Header>
   </Header>
   <Summary ID="1">
      <ID>1</ID>
      <Batch_Date>0228</Batch_Date>
      <Net_Sales_Amount>340.00</Net_Sales_Amount>
      <DATA_From_2>
         <Sales_Amount>340.00</_Sales_Amount>
      </DATA_From_2>
   </Summary>
   <Summary ID="1">
      <ID>1</ID>
      <Batch_Date>0228</Batch_Date>
      <Batch_Number>04002</Batch_Number>
      <Net_Sales_Amount>130.00</Net_Sales_Amount>
      <DATA_From_2>
         <Sales_Amount>130.00</Sales_Amount>
      </DATA_From_2>
      <DATA_From_8>
         <Invoice_Amount>20</Invoice_Amount>
         <Invoice_Number>123</Invoice_Number>
      </DATA_From_8>
      <DATA_From_8>
         <Invoice_Amount>15</Invoice_Amount>
         <Invoice_Number>456</Invoice_Number>
      </DATA_From_8>
   </Summary>
   <Summary ID="2">
      <ID>2</ID>
      <Batch_Date>0228</Batch_Date>
      <Net_Sales_Amount>40.00</Net_Sales_Amount>
      <DATA_From_2>
         <Sales_Amount>40.00</Sales_Amount>
      </DATA_From_2>
   </Summary>
   <Summary ID="2">
      <ID>2</ID>
      <Batch_Date>0228</Batch_Date>
      <Net_Sales_Amount>819.91</Net_Sales_Amount>
      <DATA_From_2>
         <Sales_Amount>734.91</Sales_Amount>
      </DATA_From_2>
      <DATA_From_5>
         <Sales_Amount>85.00</Sales_Amount>
      </DATA_From_5>
   </Summary>
</Root>

这是我的XSLT文件。我接近我想要的输出,但它没有组合相同的子元素,而是将它们全部放在Summary元素中。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />

    <xsl:template match="/*">
        <xsl:copy>
            <Root>
                <xsl:for-each-group select="/Root/Summary" group-by="@ID">
                    <Summary ID="{@ID}">
                        <!-- Copy values that are the same -->
                        <xsl:sequence select="ID, Batch_Date"/>

                        <!-- Sum the amounts -->
                        <Net_Sales_Amount>
                            <xsl:value-of select="format-number(sum(current-group()/Net_Sales_Amount), '0.00')"/>
                        </Net_Sales_Amount>

                        <!-- Combine the record types in this Summary element -->
<!--                        <xsl:sequence select="DATA_From_2, DATA_From_3, DATA_From_5, DATA_From_8"/> -->

                        <xsl:for-each-group select="current-group()" group-by="DATA_From_2">
                            <Data_From_2>
                                <Sales_Amount>
                                    <xsl:value-of select="format-number(sum(current-group()/Data_From_2/Combined_Sales_Amount), '0.00')"/>
                                </Sales_Amount>
                            </Data_From_2>
                        </xsl:for-each-group>

                        <xsl:for-each-group select="current-group()" group-by="DATA_From_8">
                            <Data_From_8>
                                <Invoice_Amount>
                                    <xsl:value-of select="format-number(sum(current-group()/Data_From_8/Invoice_Amount), '0.00')"/>
                                </Invoice_Amount>
                                <Invoice_Number>
                                    <xsl:value-of select="current-group()/Data_From_8/Invoice_Number" separator=" | "/>
                                </Invoice_Number>
                            </Data_From_8>
                        </xsl:for-each-group>
                    </Summary>
                </xsl:for-each-group>

                <Header>
                </Header>
                <Footer>
                </Footer>
            </Root>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这是我目前的输出:

<Root>
   <Summary ID="1">
      <ID>1</ID>
      <Batch_Date>0228</Batch_Date>
      <Net_Sales_Amount>470.00</Net_Sales_Amount>
      <Data_From_2>
         <Sales_Amount>0.00</Sales_Amount>
      </Data_From_2>
      <Data_From_2>
         <Sales_Amount>0.00</Sales_Amount>
      </Data_From_2>
      <Data_From_8>
         <Invoice_Amount>0.00</Invoice_Amount>
         <Invoice_Number/>
      </Data_From_8>
      <Data_From_8>
         <Invoice_Amount>0.00</Invoice_Amount>
         <Invoice_Number/>
      </Data_From_8>
   </Summary>
   <Summary ID="2">
      <ID>2</ID>
      <Batch_Date>0228</Batch_Date>
      <Net_Sales_Amount>859.91</Net_Sales_Amount>
      <Data_From_2>
         <Sales_Amount>0.00</Sales_Amount>
      </Data_From_2>
      <Data_From_2>
         <Sales_Amount>0.00</Sales_Amount>
      </Data_From_2>
      <Data_From_2>
         <Sales_Amount>0.00</Sales_Amount>
      </Data_From_2>
   </Summary>
</Root>

XSLT没有组合相同的Data_From元素,也没有正确填充值。

这应该是这样的:

<Root>
   <Summary ID="1">
      <ID>1</ID>
      <Batch_Date>0228</Batch_Date>
      <Net_Sales_Amount>470.00</Net_Sales_Amount>
      <Data_From_2>
         <Sales_Amount>470.00</Sales_Amount>
      </Data_From_2>
      <Data_From_8>
         <Invoice_Amount>35.00</Invoice_Amount>
         <Invoice_Number/>
      </Data_From_8>
   </Summary>
   <Summary ID="2">
      <ID>2</ID>
      <Batch_Date>0228</Batch_Date>
      <Net_Sales_Amount>859.91</Net_Sales_Amount>
      <Data_From_2>
         <Sales_Amount>771.91</Sales_Amount>
      </Data_From_2>
      <DATA_From_5>
         <Sales_Amount>85.00</Sales_Amount>
      </DATA_From_5>
   </Summary>
</Root>

XSLT应该组合相似的元素(Data_from_2,Data_from_5等)并将值相加。 我为任何拼写错误或不正确的XML道歉,我做了很多复制和粘贴。 感谢

1 个答案:

答案 0 :(得分:2)

我认为以下是你想要的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />

    <xsl:template match="/*">
        <xsl:copy>
            <Root>
                <xsl:for-each-group select="/Root/Summary" group-by="@ID">
                    <Summary Merchant_ID="{@ID}">
                        <!-- Copy values that are the same -->
                        <xsl:sequence select="ID, Batch_Date"/>

                        <!-- Sum the amounts -->
                        <Net_Sales_Amount>
                            <xsl:value-of select="format-number(sum(current-group()/Net_Sales_Amount), '0.00')"/>
                        </Net_Sales_Amount>

                        <!-- Combine the record types in this Summary element -->
<!--                        <xsl:sequence select="DATA_From_2, DATA_From_3, DATA_From_5, DATA_From_8"/> -->

                        <xsl:for-each-group select="current-group()/*[starts-with(local-name(), 'DATA_')]" group-by="node-name(.)">
                           <xsl:apply-templates select="."/>
                         </xsl:for-each-group>

                    </Summary>
                </xsl:for-each-group>

                <Header>
                </Header>
                <Footer>
                </Footer>
            </Root>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="DATA_From_2 | DATA_From_5">
                            <xsl:copy>
                                <Sales_Amount>
                                    <xsl:value-of select="format-number(sum(current-group()/Sales_Amount), '0.00')"/>
                                </Sales_Amount>
                            </xsl:copy>
    </xsl:template>

    <xsl:template match="DATA_From_8">
                            <xsl:copy>
                                <Invoice_Amount>
                                    <xsl:value-of select="format-number(sum(current-group()/Invoice_Amount), '0.00')"/>
                                </Invoice_Amount>
                                <Invoice_Number>
                                    <xsl:value-of select="current-group()/Invoice_Number" separator=" | "/>
                                </Invoice_Number>
                            </xsl:copy>
    </xsl:template>
</xsl:stylesheet>