string 函数 (XPath)

将对象转换为字符串。

string string(object?)

备注

将节点集转换为字符串

string() 函数通过返回节点集中第一个节点的字符串值,将节点集转换为字符串,有时,这样做可能会产生意外的结果。 例如,如果你位于下面的 <test> 节点上:

<test>
  <item>Apple</item>
  <item>Banana</item>
  <item>Orange</item>
</test>

以下 string() 函数调用返回第一个节点字符串(“Apple”):

string(//text()) 

如果希望 string() 函数将所有子文本串联起来,必须传递单个节点,而不是节点集。 例如,以下 string() 函数调用将返回“AppleBananaOrange”:

string(.)

所有接受字符串参数的 XPath 函数都是如此。 例如,以下 contains() 函数调用:

contains(//text(),'Banana')

返回值 false。 在此示例中,出现这种情况的原因是,第一个参数(“//text()”)使用 string(//text()) 转换为字符串,只会搜索第一个节点字符串(“Apple”)。 反之,如果 contains() 函数修改为第一个参数使用点选择符(“.”),如下所示:

contains(.,'Banana') 

contains() 返回的值将为 true,因为将搜索字符串“AppleBananaOrange”。

备注

如果节点集为空,则返回一个空字符串。

在节点集转换中处理空白

包含空白的节点集可以通过下列方式之一进行处理:

例如,如果 .//text() 表达式应用于以下元素,以选择其内部文本内容:

<name>element</name>

默认情况下,将返回如下所示的三个节点的节点集,其中第一个和第三个节点表示实际文本数据(“element”)旁边的前导和尾随空白节点:

Node 1: &#10;&#32;&#32;
Node 2: element
Node 03: &#10;

要忽略纯空白节点,可以如下所示在 XSLT 样式表中指定 <xsl:strip-space> 指令:

<xsl:strip-space elements="name"/>

另外,可以使用如下所示的 <xsl:for-each> 循环示例循环通过每个子代节点并反复进行搜索:

<xsl:for-each select=".//text()">
    <xsl:if test="contains(., 'element')" >
    …
    </xsl:if>
</xsl:for-each>

将数字转换为字符串

如下所示将数字转换为字符串。

  • NaN 转换为字符串 NaN。

  • 正零转换为字符串“0”。

  • 负零转换为字符串“0”。

  • 正无穷大转换为字符串“Infinity”。

  • 负无穷大转换为字符串“-Infinity”。

  • 如果数字是整数,数字以十进制格式表示,没有小数点,没有前导零,如果数字是负数,前面有一个减号 (-)。

  • 否则,数字以十进制格式表示,包含小数点,并且小数点前面至少有一位,小数点后面也至少有一位,如果数字是负数,前面有一个减号 (-);小数点前面不得包含前导零,除非小数点前面必须有一位;除了小数点后面必须包含的一位之外,必须还包含所需的相应位数,用于将该数字与所有其他 IEEE 754 数值区分开来。

备注

在向用户显示时,不能使用 string() 函数将数字转换为字符串。XSL 转换 (XSLT) 中的 format-number() 函数和 <xsl:number> 元素提供此功能。

将布尔值转换为字符串

布尔值 false 转换为字符串“false”。 布尔值 true 转换为字符串“true”。

将对象转换为字符串

不属于四种基本类型的对象通过该类型相应的方式转换为字符串。

如果省略了该参数,默认的节点集中唯一的成员是上下文节点。

示例

以下示例阐释如何在 XPath 表达式中使用 string() 函数。 在两种情况下(见下面 XSLT 文件中的加粗说明),该函数用于确保其参数被视为字符串表达式。

XML 文件 (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 文件 (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>

ms256133.collapse_all(zh-cn,VS.120).gif输出

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

请参见

参考

XML 数据类型引用

format-number 函数

概念

NaN 值 (XSLT)