2014-12-02 28 views
1

算特定節點,我有以下XML如何使用XSLT

<color> 
    <title>white</title> 
</color> 
<color> 
    <title>black</title> 
</color> 
<color> 
    <title>white</title> 
</color> 
<color> 
    <title>black</title> 
</color> 
<color> 
    <title>white</title> 
</color> 

我需要得到colorcount節點,其中title等於「white」使用xslt

即得到結果:

謝謝

回答

1

解決方案:count(color[./title='white'])

請嘗試以下代碼

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> 
<xsl:template match="*"> 
    <Value> 
     <xsl:value-of select="count(color[./title='white'])"/> 
    </Value> 
</xsl:template> 
</xsl:stylesheet> 

輸出:<Value>3</Value>

1

您需要一些XSLT才能開始,並且需要有效的XML(只有一個根元素)。

而且我需要你,爲了給你一個完整的答案提供這兩個的,但本質上,你可以使用count()功能和謂詞:

<xsl:value-of select="count(//color[title = 'white'])" /> 

更完整的例子:

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

    <xsl:template match="/"> 
     <n> 
     <xsl:value-of select="count(//color[title = 'white'])"/> 
     </n> 
    </xsl:template> 
</xsl:stylesheet>