共用方式為


string 函式

將物件轉換為字串。

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 值。此範例之所以會發生這種狀況,是因為系統會使用 string(//text()) 將第一個引數 ("//text()") 轉換為字串,因此它只會搜尋第一個節點字串 ("Apple")。相反地,若修改 contains() 函式來使用逗點選取器 ("."),則第一個引數如下:

contains(.,'Banana') 

contains() 傳回的值為 true,因為它會轉為搜尋字串 "AppleBananaOrange"。

注意

如果節點集是空的,則傳回空字串。

在節點集轉換中處理泛空白字元的方式

可利用下列任一種方式處理內含泛空白字元的節點集:

例如,若已在下列項目中套用 .//text() 運算式來選取其內部文字內容:

<name>element</name>

則它接下來預設會傳回一組節點 (內含 3 個節點),其中的第一個和第三個節點代表開頭與結尾的泛空白字元節點,而這兩個節點與實際的文字資料 ("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>

輸出

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 值