string-length() Function |
Returns the number of characters in the string passed in as the argument to this function. If no argument is specified, the context node is converted to a string and the length of that string is returned.
Inputs
An optional string.
Output
The number of characters defined in the string.
Defined in
XPath section 4.2, String Functions.
Example
The following example demonstrates the results of invoking the string-length() function against various argument types. Here's the XML document we'll use for our example:
    <?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>
We'll process this document with the following stylesheet:
    <?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>
Here are the results of our 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.
When we invoked the string-length() function without any arguments, the context node was converted to a string, then the length of that string was returned. The two <question> elements were handled this way inside the <xsl:for-each> element.