次の方法で共有


normalize-space 関数

前後の空白と連続する空白を除去した引数文字列を返します。

string normalize-space(string)

解説

前後の空白を除去し、連続する空白文字を 1 つの空白に置き換えることによって空白を正規化します。引数を省略すると、コンテキスト ノードの文字列値が正規化され、返されます。

次の関数の呼び出しでは、"abc def" が返ります。

normalize-space(" abc def ")

引数が文字列型でない場合は、文字列に変換された後、評価されます。**下記の例を参照してください。

引数が文字列型でない場合は、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 データ型リファレンス