string() Function | |
Returns the string value of the argument. | |
Inputs | |
An object. The object is converted to a string, as described in the following subsection. |
|
Output | |
A string. The input argument is converted to a string as follows:
|
|
Defined in | |
XPath section 4.2, String Functions. |
|
Example | |
Here is the XML document we'll use to test the string() function: <?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 test the string() function with a variety of arguments: <?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() function:</xsl:text> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text> string(count(/test))=</xsl:text> <xsl:value-of select="string(count(/test))"/> <xsl:value-of select="$newline"/> <xsl:text> string(count(/test/question))=</xsl:text> <xsl:value-of select="string(count(/test/question))"/> <xsl:value-of select="$newline"/> <xsl:text> string('4')=</xsl:text> <xsl:value-of select="string('4')"/> <xsl:value-of select="$newline"/> <xsl:text> string(true())=</xsl:text> <xsl:value-of select="string(true())"/> <xsl:value-of select="$newline"/> <xsl:text> string(false())=</xsl:text> <xsl:value-of select="string(false())"/> <xsl:value-of select="$newline"/> <xsl:text> string(count(/test/question) > 5)=</xsl:text> <xsl:value-of select="string(count(/test/question) > 5)"/> <xsl:value-of select="$newline"/> <xsl:value-of select="$newline"/> <xsl:text>Here are the string values of some <text> elements:</xsl:text> <xsl:value-of select="$newline"/> <xsl:for-each select="/test/question/text"> <xsl:text> </xsl:text> <xsl:value-of select="string(.)"/> <xsl:value-of select="$newline"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> Here are the results of our stylesheet: Tests of the string() function: string(count(/test))=1 string(count(/test/question))=2 string('4')=4 string(true())=true string(false())=false string(count(/test/question) > 5)=false Here are the string values of some <text> elements: When completed, the Eiffel Tower was the tallest building in the world. New York's Empire State Building knocked the Eiffel Tower from its pedestal. |