2017-02-23 61 views
0

===================================== ===================================如何使用XSLT 1.0計算重複元素的總和

**Input XML:** 
<Customers> 
<Customer> 
    <Items> 
     <Item> 
      <ItemId>5639</ItemId> 
      <ProductName> Lawnmower </ProductName> 
      <Quantity>1</Quantity> 
      <USPrice>148.95</USPrice> 
      <Comment>Confirm this is electric</Comment> 
     </Item> 
     <Item> 
      <ItemId>6000</ItemId> 
      <ProductName>Baby Monitor</ProductName> 
      <Quantity>2</Quantity> 
      <USPrice>39.98</USPrice> 
      <Comment>Confirm this is electric</Comment> 
     </Item> 
    </Items> 
</Customer> 
<Customer> 
    <Items> 
     <Item> 
      <ItemId>7000</ItemId> 
      <ProductName> Cutter </ProductName> 
      <Quantity>1</Quantity> 
      <USPrice>15.95</USPrice> 
      <Comment>Confirm this is electric</Comment> 
     </Item> 
     <Item> 
      <ItemId>9000</ItemId> 
      <ProductName> Laptop </ProductName> 
      <Quantity>5</Quantity> 
      <USPrice>2048.95</USPrice> 
      <Comment>Confirm this is electric</Comment> 
     </Item> 
    </Items> 
</Customer > 

輸出XML:

<Customers> 
    <Customer> 
     <TotalPurchase>sum(USPRice)</TotalPurchase> 
    </Customer> 
    <Customer> 
     <TotalPurchase>sum(USPrice)</TotalPurchase> 
    </Customer> 
</Customers> 

XSLT代碼:

<Customers> 
    <xsl:for-each select="Customers/Customer"> 
    <Customer> 
     <xsl:for-each select="Items/Item"> 
      <TotalPurchase> 
       <xsl:value-of select="sum(/USPrice)"/> 
      </TotalPurchase> 
     </xsl:for-each> 
    </Customer> 
    </xsl:for-each> 
</Customers> 

有人可以指導我如何在XSLT 1.0中實現輸出。由於總和功能沒有正常工作。它恢復零。

+0

「*總和功能did not工作*」發佈您的嘗試。請提供一個格式良好的XML作爲輸入示例:「客戶1」不是有效的元素名稱。 –

+0

您是想將現在的價格相加,還是將價格乘以數量? –

+0

感謝您的回覆。 這是我的XSLT代碼。我只需要每個專業人員的項目總和。不是通過乘以數量。 ** XSLT代碼:** <的xsl:for-每個選擇= 「顧客/顧客」> <的xsl:for-每個選擇= 「項目/項目」> < XSL:value-of的選擇= 「SUM(/ USPrice)」/> ranaa

回答

1

如果你想求和USPrice值爲每個Customer,不受Quantity第一(這似乎很奇怪,我)乘以它們,然後執行:

<xsl:template match="/Customers"> 
    <xsl:copy> 
     <xsl:for-each select="Customer"> 
      <Customer> 
       <TotalPurchase> 
        <xsl:value-of select="sum(Items/Item/USPrice)"/> 
       </TotalPurchase> 
      </Customer> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

您示例中的結果將爲:

<Customers> 
    <Customer> 
    <TotalPurchase>188.93</TotalPurchase> 
    </Customer> 
    <Customer> 
    <TotalPurchase>2064.9</TotalPurchase> 
    </Customer> 
</Customers> 
+0

謝謝michael。您的解決方案奏效 – ranaa

0

您輸入XML看起來並不有效 - 例如,如果你希望客戶標籤內的值或標籤

e.g. <customer id=1> or <customer><id>1</id> 

**更新裏面的id元素,你要麼需要一個屬性名** 您的XPath的選擇是不正確 - 一旦你已經完成了換每個項目上/項目你不需要斜線來指代子元素 因此您的代碼應閱讀

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

但這不會解決你的問題。在for-each中,您一次只指向一個「item」節點,因此您一次只能獲得一個USPrice的總和。

我相信

<xsl:for-each select="Customers/Customer"> 
    <Customer> 
      <TotalPurchase> 
       <xsl:value-of select="sum(Items/Item/USPrice)"/> 
      </TotalPurchase> 
    </xsl:for-each> 

應該工作

+0

這不提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/15312430) –

+0

@Regis - 這很公平我已經根據提問者提供的XML更新了我的答案。這似乎很奇怪,我需要更多的聲譽來作出澄清評論比我回答一個問題 – RT72

+0

我不寫SO規則,但我想的重點是要避免評論氾濫(這是沒有審查)。 –

0

讓我們清楚你做錯了什麼,所以你不要再犯同樣的錯誤。

 <xsl:for-each select="Items/Item"> 
      <TotalPurchase> 
       <xsl:value-of select="sum(/USPrice)"/> 
      </TotalPurchase> 
     </xsl:for-each> 

你做錯的第一件事是,「/」 USPrice之前。 「/」會將您帶到您不想要的文檔的根目錄 - 您想從當前位置向下選擇一個Item元素。所以放下「/」。

但是這並沒有解決問題。 sum()函數添加一組數字,但從Item開始的USPrice僅選擇一個數字。你的錯誤在於認爲「我正在處理一系列事情,因此我需要循環它們」。忘記所有這些低層次的程序思維,你在這裏處理的是一種高層次的聲明性語言。你想要的是:

 <TotalPurchase> 
      <xsl:value-of select="sum(Items/Item/USPrice)"/> 
     </TotalPurchase>