count() 関数 | |
指定されたノードセット内のノード数をカウントします。 | |
入力 | |
ノードセット。 |
|
出力 | |
ノードセットに含まれるノードの数。 |
|
定義先 | |
XPath 4.1 節「ノードセット関数」 |
|
例 | |
count() 関数を確認するために使用する XML ドキュメントを次に示します。 <?xml version="1.0"?> <test> <p>This is a test XML document used by several of our sample stylesheets.</p> <question> <text>When completed, the Eiffel Tower was the tallest building in the world.</text> <true>You're correct! The Eiffel Tower was the world's tallest building until 1930.</true> <false>No, the Eiffel Tower was the world's tallest building for over 30 years.</false> </question> <question> <text>New York's Empire State Building knocked the Eiffel Tower from its pedestal.</text> <true>No, that's not correct.</true> <false>Correct! New York's Chrysler Building, completed in 1930, became the world's tallest.</false> </question> </test> count() 関数を使用するスタイルシートを次に示します。 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:text>Tests of the count() function:</xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text> count(/test)=</xsl:text> <xsl:value-of select="count(/test)"/> <xsl:value-of select="$newline"/> <xsl:text> count(/true)=</xsl:text> <xsl:value-of select="count(/true)"/> <xsl:value-of select="$newline"/> <xsl:text> count(//true)=</xsl:text> <xsl:value-of select="count(//true)"/> <xsl:value-of select="$newline"/> <xsl:text> count(//test|//true|//text)=</xsl:text> <xsl:value-of select="count(//test|//true|//text)"/> <xsl:value-of select="$newline"/> <xsl:variable name="numberOfQuestions" select="count(/test/question)"/> <xsl:for-each select="/test/question"> <xsl:text> This is question number </xsl:text> <xsl:value-of select="position()"/> <xsl:text> of </xsl:text> <xsl:value-of select="$numberOfQuestions"/> <xsl:value-of select="$newline"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> このスタイルシートの結果は次のとおりです。 Tests of the count() function: count(/test)=1 count(/true)=0 count(//true)=2 count(//test|//true|//text)=5 This is question number 1 of 2 This is question number 2 of 2 count() 関数の最初の 4 つの呼び出しでは、単に XPath 式を使用して XML ドキュメント内の要素をカウントします。count() の最後の呼び出しでは、ドキュメント内の <question> 要素の数をカウントし、その値を変数に格納します。 |