共用方式為


normalize-space 函式

傳回除掉前導、結尾與重複泛空白字元的引數字串。

string normalize-space(string)

備註

泛空白字元的正規化方式是除掉前導和結尾泛空白字元,並以單一空格取代泛空白字元序列。如果省略引數,則會正規化並傳回內容節點的字串值。

下列函式呼叫會傳回 "abc def":

normalize-space(" abc def ")

如果引數不是型別字串,就會先將引數轉換成字串後,再予以評估。請參閱下列範例。

如果引數的型別不是 string,則會先使用 string() 函式將它轉換成字串,然後再評估該轉換的結果。

注意

當作引數傳遞至此函式的節點集字串轉換,有可能會產生非預期的結果。如需詳細資訊,請參閱 string 函式

此函式有區分大小寫。

範例

下列範例可正規化含未正規化泛空白字元 (索引標籤、前導空格和結尾空格,以及字組之間的多個空格) 的文字字串區塊。文字字串是 <text> 項目的值。

XML 檔 (normSpace.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" 

href="normalizeSpace.xsl"?>      
<text>
    This is a
  test, with a   lot    of 
    irregular  spacing and
    waiting to be   normalizaed.


</text>

XSLT 檔 (normSpace.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"           
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml"   
       omit-xml-declaration="yes"/>

<xsl:template match="/text">
Unnormalized: 
"<xsl:value-of select='.'/>"
Normalized: "<xsl:value-of select='normalize-space()'/>"
</xsl:template>

</xsl:stylesheet>

這個 XSLT 產生以下輸出:

Unormalized:

"

This is a

test, with a lot of

irregular spacing and

waiting to be normalizaed.

"

Normalized:

"This is a test, with a lot of irregular spacing and waiting to be normalized."

請參閱

參考

XML 資料型別參考