2010-09-08 46 views
1

卡住位具有以下並會喜歡一些想法/指針在正確的方向。我有以下XML:取計數(XSLT 1)

<RecordsCollection> 
    <CustomerRecord> 
    <customerId>12345</customerId> 
    <currency>USD</currency> 
    </CustomerRecord> 
    <CustomerRecord> 
    <customerId>12345</customerId> 
    <currency>USD</currency> 
    </CustomerRecord> 
    <CustomerRecord> 
    <customerId>90210</customerId> 
    <currency>USD</currency> 
    </CustomerRecord> 
</RecordsCollection> 

我需要做到的是簡單地生產含有包含了獨有的賬號,獨一無二的貨幣元素的個數單個值。爲了進一步解釋,上面的示例包含兩個具有相同貨幣(USD)的相同帳號(12345)的條目,因此它們被計爲1,另一個條目也被計爲1.因此,上面的示例應該爲在:

<totalCount>2</totalCount> 

任何想法如何實現它?我知道如何選擇不同的賬戶號碼,但是我似乎無法將我的頭腦包裹起來,那就是如何計算每個賬戶中不同的貨幣。

最後,這必須使用XSLT 1.0完成了...任何想法將不勝感激!

+0

問得好(+1)。看到我的答案是一個有效和簡短的解決方案。 :) – 2010-09-08 19:56:45

回答

0

這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:key name="kRecordByIdAndCurr" match="CustomerRecord" 
      use="concat(customerId,'++',currency)"/> 
    <xsl:template match="/"> 
     <totalCount> 
      <xsl:value-of select="count(*/* 
             [count(.|key('kRecordByIdAndCurr', 
                 concat(customerId, 
                  '++', 
                  currency) 
                )[1] 
               )=1])"/> 
     </totalCount> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<totalCount>2</totalCount> 

注:僅僅通過customerIdcurrency字符串值分組。

編輯:對不起,錯過了customerId之前。

+0

這就是 - 完全是我需要它做的,非常感謝你! – user442749 2010-09-08 19:48:21

+0

@ user442749:你好! – 2010-09-08 20:03:28

0

這種轉變

<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="kCurrencyByIdAndType" match="currency" 
    use="concat(../customerId, '+', .)"/> 

<xsl:template match="/*"> 
    <totalCount> 
    <xsl:value-of select= 
    "count(*/currency[generate-id(key('kCurrencyByIdAndType', 
             concat(../customerId, '+', .) 
            )[1] 
           ) 
        = generate-id() 
        ] 
     ) 
    "/> 
    </totalCount> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<RecordsCollection> 
    <CustomerRecord> 
    <customerId>12345</customerId> 
    <currency>USD</currency> 
    </CustomerRecord> 
    <CustomerRecord> 
    <customerId>12345</customerId> 
    <currency>USD</currency> 
    </CustomerRecord> 
    <CustomerRecord> 
    <customerId>90210</customerId> 
    <currency>USD</currency> 
    </CustomerRecord> 
</RecordsCollection> 

產生想要的,正確的結果

<totalCount>2</totalCount> 

說明: Muenchian分組方法用於組合鍵,可以表示爲任何currency及其兄弟姐妹的字符串值的串聯。

+0

謝謝Dimitre !!!這是完美的,以及... – user442749 2010-09-08 20:37:53

+0

親愛的@ user442749,在SO感恩表示爲upvoting(這個你就可以當你獲得50點聲望做),並通過接受一個答案(這個你可以* * *做現在* )。只需點擊答案左邊的複選標記即可接受。 :) – 2010-09-08 20:53:40