count 函式 (XPath)
傳回節點集引數中的節點數。
number count(node-set)
範例
XML 檔 (test.xml)
<?xml version="1.0"?>
<test>
<x a="1">
<x a="2">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
</x>
<x a="1">
<x a="2">
<y>y21</y>
<y>y22</y>
</x>
</x>
<x a="1">
<y>y11</y>
<y>y12</y>
</x>
<x>
<y>y03</y>
<y>y04</y>
</x>
</test>
XSLT 檔 (test.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
//x, <xsl:value-of select="count(//x)"/>
//x[1], <xsl:value-of select="count(//x[1])"/>
//x/y, <xsl:value-of select="count(//x/y)"/>
//x/y[1], <xsl:value-of select="count(//x/y[1])"/>
//x[1]/y[1], <xsl:value-of select="count(//x[1]/y[1])"/>
</xsl:template>
</xsl:stylesheet>
輸出
將 XSLT 樣式表套用到以上的 XML 檔時會產生下列結果:
節點集 |
count(node-set) |
---|---|
//x |
7 |
//x[1] |
4 |
//x/y |
8 |
//x/y[1] |
4 |
//x[1]/y[1] |
2 |