string Function
Converts an object to a string.
string string(object?)
Remarks
Converting Node Sets to Strings
The string() function converts a node-set to a string by returning the string value of the first node in the node-set, which in some instances may yield unexpected results. For example, if you are positioned on the <test> node below:
<test>
<item>Apple</item>
<item>Banana</item>
<item>Orange</item>
</test>
Then the following string() function call returns the first node string ("Apple"):
string(//text())
If you want the string() function to concatenate all child text, you must then pass a single node instead of a node-set. For example, the following string() function call would return "AppleBananaOrange":
string(.)
This behavior holds true for all XPath functions that take string arguments. For example, the following contains() function call:
contains(//text(),'Banana')
returns a value of false. In this example, this occurs because the first argument ("//text()
") is converted to a string using string(//text())
it only searches the first node string ("Apple"). In contrast, if the contains() function were revised to use the dot selector (".") for the first argument as follows:
contains(.,'Banana')
the value returned by contains() would be true because it would search the string "AppleBananaOrange" instead.
Note
If the node-set is empty, an empty string is returned.
Handling Whitespace in Node-Set Conversions
Node sets that contain whitespace can be handled by either:
Stripping out the whitespace nodes using a <xsl:strip-space> Element instruction
Looping through all descendent nodes (both whitespace nodes and nodes with text) using an <xsl:for-each> Element instruction.
For example, if a .//text()
expression were applied to the following element to selects its inner text contents:
<name>element</name>
it would, by default, return a set of three nodes as follows where the first and third nodes represent preceding and trailing whitespace nodes adjacent to the actual text data ("element"):
Node 1:   
Node 2: element
Node 03:
To ignore the whitespace-only nodes you could specify the <xsl:strip-space> instruction as follows in the XSLT stylesheet:
<xsl:strip-space elements="name"/>
Alternately, you could loop through and repeat a search for each descendant node using the <xsl:for-each> loop example shown below:
<xsl:for-each select=".//text()">
<xsl:if test="contains(., 'element')" >
…
</xsl:if>
</xsl:for-each>
Converting Numbers to Strings
A number is converted to a string as follows.
NaN is converted to the string NaN.
positive zero is converted to the string "0".
negative zero is converted to the string "0".
positive infinity is converted to the string "Infinity".
negative infinity is converted to the string "-Infinity".
If the number is an integer, the number is represented in decimal form as a number with no decimal point and no leading zeros, preceded by a minus sign (-) if the number is negative.
Otherwise, the number is represented in decimal form as a number with a decimal point and at least one digit before the decimal point and at least one digit after the decimal point, preceded by a minus sign (-) if the number is negative; there must be no leading zeros before the decimal point, apart possibly from the one required digit immediately before the decimal point; beyond the one required digit after the decimal point there must be as many, but only as many, more digits as are needed to uniquely distinguish the number from all other IEEE 754 numeric values.
Note
The string()
function is not intended for converting numbers into strings for presentation to users. The format-number()
function and <xsl:number>
element in XSL Transformations (XSLT) provide this functionality.
Converting Booleans to Strings
The Boolean false value is converted to the string "false". The Boolean true value is converted to the string "true".
Converting Objects to Strings
An object of a type other than the four basic types is converted to a string in a way that is dependent on that type.
If the argument is omitted, it defaults to a node-set with the context node as its only member.
Example
The following example illustrates how to use the string()
function in an XPath expression. In two cases (see the instructions in bold in the XSLT file below), the function is used to ensure that its argument is treated as a string expression.
XML File (string.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="string.xsl"?>
<arithmetics>
<operation>
<operator>+</operator>
<operand>1</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>+</operator>
<operand>One</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>-</operator>
<operand>1</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>*</operator>
<operand>1</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>div</operator>
<operand>-1</operand>
<operand>0.0</operand>
</operation>
<operation>
<operator>mod</operator>
<operand>5</operand>
<operand>2</operand>
</operation>
<operation>
<operator>mod</operator>
<operand>5</operand>
<operand>2.5</operand>
</operation>
<operation>
<operator>mod</operator>
<operand>5</operand>
<operand>2.25</operand>
</operation>
<operation>
<operator>&</operator>
<operand>0</operand>
<operand>1</operand>
</operation>
</arithmetics>
XSLT File (string.xsl)
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="string.xsl"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
omit-xml-declaration="yes"/>
<xsl:template match="/arithmetics">
<html>
<head><title>example</title></head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="operation">
<DIV>
<xsl:choose>
<xsl:when test="string(operator)='+'">
<xsl:apply-templates select="." mode="add"/>
</xsl:when>
<xsl:when test="string(operator)='-'">
<xsl:apply-templates select="." mode="sub"/>
</xsl:when>
<xsl:when test="string(operator)='*'">
<xsl:apply-templates select="." mode="mul"/>
</xsl:when>
<xsl:when test="string(operator)='div'">
<xsl:apply-templates select="." mode="div"/>
</xsl:when>
<xsl:when test="string(operator)='mod'">
<xsl:apply-templates select="." mode="mod"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="." mode="err"/>
</xsl:otherwise>
</xsl:choose>
</DIV>
</xsl:template>
<xsl:template match="operation" mode="show">
<xsl:value-of select="operand[1]"/>  
<xsl:value-of disable-output-escaping="yes"
select="string(operator)"/>  
<xsl:value-of select="operand[2]"/>  
=  
</xsl:template>
<xsl:template match="operation" mode="err">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="string('Invalid arithmetic operation')"/>
</xsl:template>
<xsl:template match="operation" mode="add">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] + operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="sub">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] - operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="mul">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] * operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="div">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] div operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="mod">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] mod operand[2]"/>
</xsl:template>
</xsl:stylesheet>
Output
1 + 2.00 = 3 One + 2.00 = NaN 1 - 2.00 = -1 1 * 2.00 = 2 -1 div 0.0 = -Infinity 5 mod 2 = 1 5 mod 2.5 = 0 5 mod 2.25 = 0.5 0 & 1 = Invalid arithmetic operation
See Also
Reference
XML Data Types Reference
format-number Function