我正在使用for-each-group
組合使用for-each-group
的XML元素,並且我遇到的問題是這些XML元素的子元素具有自己的子元素。 我也想結合兒童元素。在分組父元素內對子元素進行分組XSLT
我輸入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,我表示歉意,我做了很多複製和粘貼。 感謝
怎麼來的'<摘要MERCHANT_ID = 「{@ MERCHANT_ID}」>'農產品'<摘要ID = 「1」>' – vels4j
@ vels4j - 這是一個複製粘貼錯誤。我經歷了並嘗試使名字簡單易懂。我現在修好了。 – RXC