string-length() 関数 | |
この関数のパラメータとして渡された文字列の文字数を返します。パラメータを指定しないと、コンテキストノードが文字列に変換され、その文字列の長さが返されます。 | |
入力 | |
省略可能な文字列。 |
|
出力 | |
文字列で定義された文字数。 |
|
定義先 | |
XPath 4.2 節「文字列関数」 |
|
例 | |
次の例では、さまざまなパラメータタイプに対して string-length() 関数を呼び出した結果を示しています。例で使用する 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> 次のスタイルシートを使用してこのドキュメントを処理します。 <?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 string-length() function:</xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text> string-length(/test)=</xsl:text> <xsl:value-of select="string-length(/test)"/> <xsl:value-of select="$newline"/> <xsl:text> string-length(/true)=</xsl:text> <xsl:value-of select="string-length(/true)"/> <xsl:value-of select="$newline"/> <xsl:text> string-length(//true)=</xsl:text> <xsl:value-of select="string-length(//true)"/> <xsl:value-of select="$newline"/> <xsl:text> string-length(//test|//true|//text)=</xsl:text> <xsl:value-of select="string-length(//test|//true|//text)"/> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:for-each select="/test/question"> <xsl:text> Question #</xsl:text> <xsl:value-of select="position()"/> <xsl:text> contains </xsl:text> <xsl:value-of select="string-length()"/> <xsl:text> characters.</xsl:text> <xsl:value-of select="$newline"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> このスタイルシートの結果は次のとおりです。 Tests of the string-length() function: string-length(/test)=522 string-length(/true)=0 string-length(//true)=78 string-length(//test|//true|//text)=522 Question #1 contains 239 characters. Question #2 contains 203 characters. パラメータを指定せずに string-length() 関数を呼び出したときは、コンテキストノードが文字列に変換され、その文字列の長さが返されました。2 つの <question> 要素が、<xsl:for-each> 要素内でこのような方法で処理されました。 |