Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Reference

string Function

Converts an object to a string.

string string(object?)

Remarks

An object is converted to a string as follows.

A node-set is converted to a string by returning the string value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.

A number is converted to a string as follows.

The Boolean false value is converted to the string "false". The Boolean true value is converted to the string "true".

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.

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.

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>&amp;</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]"/> &#160;
     <xsl:value-of disable-output-escaping="yes" 
                   select="string(operator)"/> &#160;
     <xsl:value-of select="operand[2]"/> &#160;
     = &#160;
  </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

Data Types in Schemas | XDR Schema Data Types Reference | XML Data Types Reference | NaN Values | format-number Function