string Function

MSXML 5.0 SDK

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.

  • 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.

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