オブジェクトを文字列に変換します。
string string(object?)
解説
ノード セットの文字列への変換
string() 関数は、ノード セット内の最初のノードの文字列値を返すことにより、ノード セットを文字列に変換します。ノード セットから文字列への変換では、予期しない結果が生じることがあります。たとえば、現在の位置に次の <test> ノードがあるとします。
<test>
<item>Apple</item>
<item>Banana</item>
<item>Orange</item>
</test>
その場合、次のように string() 関数を呼び出すと、最初のノードの文字列 ("Apple") が返ります。
string(//text())
string() 関数にすべての子テキストを連結させたい場合は、ノード セットではなく 1 つのノードを渡す必要があります。たとえば、次のように string() 関数を呼び出すと、"AppleBananaOrange" が返ります。
string(.)
この動作は、文字列引数を受け取るすべての XPath 関数に共通です。たとえば、次の contains() 関数を呼び出してみましょう。
contains(//text(),'Banana')
この場合は値 false が返ります。そうなるのは、string(//text())
を使って最初の引数 ("//text()
") を文字列に変換すると、最初のノードの文字列 ("Apple") しか検索されないからです。今度は、次のように contains() 関数を変更して、最初の引数でドット セレクタ (".") を使用してみましょう。
contains(.,'Banana')
この場合は、文字列 "AppleBananaOrange" が検索されるため、contains() が返す値が true になります。
注意
ノード セットが空である場合は、空の文字列が返されます。
ノード セットの変換における空白の処理
空白が含まれたノード セットは、次のいずれかの方法で処理できます。
<xsl:strip-space> 要素 命令を使用して空白ノードを除去する
<xsl:for-each> 要素 命令を使用して、すべての子孫ノード (空白ノードとテキストが含まれたノードの両方) をループ処理する
たとえば、.//text()
式を次の要素に適用し、内部のテキスト コンテンツを選択してみます。
<name>element</name>
この場合は、既定で、次に示す 3 つのノードの集合が返ります。最初のノードと 3 番目のノードは、実際のテキスト データ ("element") と隣接する前の空白ノードと後の空白ノードです。
Node 1:   
Node 2: element
Node 03:
空白のみのノードを無視するには、次のように、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" に変換されます。
数値が整数である場合は、小数点と頭のゼロが付かない 10 進形式の数字に変換されます。数値が負の場合は、頭にマイナス記号 (-) が付きます。
それ以外の場合は、小数点と、小数点の前後それぞれ 1 桁以上の数字から構成される 10 進形式の数字に変換されます。数値が負の場合は、頭にマイナス記号 (-) が付きます。小数点の 1 桁前にゼロを入れる必要がある場合を除いて、小数点の前に頭のゼロを入れることはできません。小数点の後ろに入れる必要がある 1 桁のほかには、その数値をその他すべての IEEE 754 数値から一意に識別するのに必要な桁より多くの桁を入れることはできません。
注意
string()
関数の目的は、数値を文字列に変換し、ユーザーに提示することではありません。XSL Transformations (XSLT) では、format-number()
関数と <xsl:number>
要素がその機能を果たします。
ブール値の文字列への変換
ブール値の false は、文字列 "false" に変換されます。ブール値の true は、文字列 "true" に変換されます。
オブジェクトの文字列への変換
4 つの基本型以外の型のオブジェクトは、型に応じた方法で文字列に変換されます。
引数が省略された場合、唯一のメンバとしてコンテキスト ノードが設定されたノード セットが既定値となります。
例
次の例では、XPath 式で string()
関数を使用する方法を説明します。2 つの事例 (次の 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>&</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]"/>  
<xsl:value-of disable-output-escaping="yes"
select="string(operator)"/>  
<xsl:value-of select="operand[2]"/>  
=  
</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 関数