2012-03-06 58 views
1

我需要總結通過我們的合作伙伴XML地點銷售(不知道有多少提前位置):如何總結xsl中每個類別的值?

樣本輸入:

<company> 
    <deparment dept="001"> 
     <location> 
      <locationCode>001</locationCode> 
      <sales>10000</sales> 
     </location> 
     <location> 
      <locationCode>0005</locationCode> 
      <sales>12500</sales> 
     </location> 
    </deparment> 
    <deparment dept="002"> 
     <location> 
      <locationCode>001</locationCode> 
      <sales>40000</sales> 
     </location> 
     <location> 
      <locationCode>004</locationCode> 
      <sales>30000</sales> 
     </location> 
    </deparment> 
    <deparment dept="003"> 
     <location> 
      <locationCode>004</locationCode> 
      <sales>60000</sales> 
     </location> 
    </deparment> 
</company> 

預期的輸出:

<location> 
    <locationCode>001</locationCode> 
    <totalSales>50000</locationCode> 
<location> 
<location> 
    <locationCode>005</locationCode> 
    <totalSales>12500</locationCode> 
<location> 
<location> 
    <locationCode>004</locationCode> 
    <totalSales>90000</locationCode> 
<location> 

不xsl有類似於group-by的內容嗎?我是xsl的新手,並感謝任何幫助。

謝謝。

+0

你使用什麼xsl版本? Xslt2.0有xsl:for-each-group指令 – Vitaliy 2012-03-06 17:25:48

+0

我們使用xslt 1.0 – user1252779 2012-03-07 15:25:36

+0

謝謝大家的幫忙! – user1252779 2012-03-07 15:27:18

回答

1

我知道它爲xslt 1.0工作。

<xsl:key name="Location" match="/company/deparment/location" use="locationCode"/> 

<xsl:template match="company"> 
    <company> 
    <xsl:for-each select="./deparment/location[generate-id() = generate-id(key('Location', ./locationCode)[1])]"> 
    <xsl:variable name="LocationCode" select="./locationCode"/> 
    <location> 
     <locationCode> 
     <xsl:value-of select="$LocationCode"/> 
     </locationCode> 
     <totalSales> 
     <xsl:value-of select="sum(//location[locationCode = $LocationCode]/sales)"/> 
     </totalSales> 
    </location> 
    </xsl:for-each> 
    </company> 
</xsl:template> 

請查看muenchian method以瞭解將來的問題。

+0

非常感謝您的幫助。它完美的工作 – user1252779 2012-03-07 15:25:05

0

對於XSLT 2.0(與的xsl:for-各個羣組指令):

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0" > 

    <xsl:output indent="yes" /> 

    <xsl:template match="/"> 
     <xsl:for-each-group select="company/deparment/location" group-by="locationCode"> 
      <location> 
       <locationCode><xsl:value-of select="current-grouping-key()"/></locationCode> 
       <totalSales><xsl:value-of select="sum(current-group()/sales)"/></totalSales> 
      </location> 
     </xsl:for-each-group> 

    </xsl:template> 

</xsl:stylesheet> 
1

這將是使用XSLT 2.0的溶液:

對於以下輸入XML:

<?xml version="1.0" encoding="UTF-8"?> 
<company> 
    <department dept="001"> 
     <location> 
      <locationCode>001</locationCode> 
      <sales>10000</sales> 
     </location> 
     <location> 
      <locationCode>0005</locationCode> 
      <sales>12500</sales> 
     </location> 
    </department> 
    <department dept="002"> 
     <location> 
      <locationCode>001</locationCode> 
      <sales>40000</sales> 
     </location> 
     <location> 
      <locationCode>004</locationCode> 
      <sales>30000</sales> 
     </location> 
    </department> 
    <department dept="003"> 
     <location> 
      <locationCode>004</locationCode> 
      <sales>60000</sales> 
     </location> 
    </department> 
</company> 

使用以下XSLT:

<?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="/company"> 
<company> 
    <xsl:for-each-group select="department/location" group-by="locationCode"> 
     <location> 
      <locationCode><xsl:value-of select="current-grouping-key()"/></locationCode> 
      <totalSales><xsl:value-of select="sum(current-group()/sales)"/></totalSales> 
     </location> 
    </xsl:for-each-group> 
</company> 
</xsl:template> 
</xsl:stylesheet> 

我得到的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<company xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <location> 
     <locationCode>001</locationCode> 
     <totalSales>50000</totalSales> 
    </location> 
    <location> 
     <locationCode>0005</locationCode> 
     <totalSales>12500</totalSales> 
    </location> 
    <location> 
     <locationCode>004</locationCode> 
     <totalSales>90000</totalSales> 
    </location> 
</company> 
1

短,簡單和有效的XSLT 1.0溶液(沒有變量,沒有xsl:for-each

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:key name="kLocById" match="location" use="locationCode"/> 

<xsl:template match= 
    "location 
    [generate-id() 
    = 
    generate-id(key('kLocById',locationCode)[1]) 
    ]"> 
    <location> 
    <xsl:copy-of select="locationCode"/> 
    <totalSales> 
    <xsl:value-of select="sum(key('kLocById',locationCode)/sales)"/> 
    </totalSales> 
    </location> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

當這種轉化應用於提供的XML文檔

<company> 
    <deparment dept="001"> 
     <location> 
      <locationCode>001</locationCode> 
      <sales>10000</sales> 
     </location> 
     <location> 
      <locationCode>0005</locationCode> 
      <sales>12500</sales> 
     </location> 
    </deparment> 
    <deparment dept="002"> 
     <location> 
      <locationCode>001</locationCode> 
      <sales>40000</sales> 
     </location> 
     <location> 
      <locationCode>004</locationCode> 
      <sales>30000</sales> 
     </location> 
    </deparment> 
    <deparment dept="003"> 
     <location> 
      <locationCode>004</locationCode> 
      <sales>60000</sales> 
     </location> 
    </deparment> 
</company> 

的希望,正確的結果產生

<location> 
    <locationCode>001</locationCode> 
    <totalSales>50000</totalSales> 
</location> 
<location> 
    <locationCode>0005</locationCode> 
    <totalSales>12500</totalSales> 
</location> 
<location> 
    <locationCode>004</locationCode> 
    <totalSales>90000</totalSales> 
</location> 

說明:適當使用Muenchian method for grouping和模板匹配的。

+0

你的解決方案完美工作。謝謝。 – user1252779 2012-03-07 15:26:20

+0

@ user1252779:我很高興我的回答對你有用。請考慮接受最佳答案(點擊答案旁邊的複選標記)。 – 2012-03-07 15:30:01