2017-09-12 48 views
0

,我有以下數據:XML如何計算基於分組

<countries> 
    <continent name="Europe"> 
     <country>France</country> 
     <country>United Kingdom</country> 
     ... 
    </continent> 
    <continent name="North America"> 
     <country>Canada</country> 
     <country>United States</country> 
     ... 
    </continent> 
</countries> 

<clients> 
    <client> 
     <name>John</name> 
     <country>Canada</country> 
    </client> 
    ... 
</clients> 

我可以像計算每個國家的客戶端數量如下:

<xsl:key name="clientcountry" match="client" use="country"/> 

<xsl:for-each select="countries//country"> 
    <xsl:value-of select="concat(., ': ', count(key('clientcountry', .)))"/> 
</xsl:for-each> 

我怎樣才能算每個大洲的客戶數量?

回答

1

嘗試

<xsl:for-each select="countries/continent"> 
    <xsl:value-of select="concat(@name, ': ', count(key('clientcountry', country)))"/> 
</xsl:for-each> 
+0

等待這是如何工作的? 「國家」是指所有潛在的國家元素?所以可以用'key()'一次輸入多個節點? –

+1

當然,請參閱https://www.w3.org/TR/xslt#function-key:「當key函數的第二個參數是node-set類型時,那麼結果就是應用對參數節點集中的每個節點的字符串值的關鍵函數「。 –