| substring() 関数 | |
| 指定された文字列の一部を返します。2 番目および 3 番目のパラメータは、返される文字列の部分を指定します。2 番目のパラメータは、部分文字列の最初の文字の位置を指定し、省略可能な 3 番目のパラメータは、返す文字数を指定します。 | |
| 入力 | |
|
substring() 関数は、文字列と、1 つまたは 2 つの数値をパラメータとして受け取ります。この文字列は、部分文字列の抽出元となる文字列です。2 番目のパラメータは、返される部分文字列の開始位置として使用され、省略可能な 3 番目のパラメータでは、返すパラメータ数を指定します。 |
|
| 出力 | |
|
substring() 関数は、2 つのパラメータ (文字列と開始位置) により、開始位置から始まる文字列内のすべての文字を返します。XPath 文字列の最初の文字は位置 0 ではなく、位置 1 にあります。 3 つのパラメータ (文字列、開始位置、および長さ) により、substring() 関数は、開始位置より位置が大きいか等しい文字列と、開始位置と長さを足したものよりも位置が小さいか等しい文字列内のすべての文字を返します。 通常、substring() 関数のパラメータは整数値ですが、より複雑な式である場合もあります。例外的なケースについては、後の例を参照してください。 |
|
| 定義先 | |
|
XPath 4.2 節「文字列関数」 |
|
| 例 | |
|
この XML ドキュメントを使用して、substring() 関数の動作を示します。
<?xml version="1.0"?>
<test>
<p>This is a test XML document used by several
of our sample stylesheets.</p>
<question>
<text>When completed, the Eiffel Tower was the
tallest building in the world.</text>
<true>You're correct! The Eiffel Tower was the
world's tallest building until 1930.</true>
<false>No, the Eiffel Tower was the world's
tallest building for over 30 years.</false>
</question>
<question>
<text>New York's Empire State Building knocked the
Eiffel Tower from its pedestal.</text>
<true>No, that's not correct.</true>
<false>Correct! New York's Chrysler Building,
completed in 1930, became the world's tallest.</false>
</question>
</test>
使用するスタイルシートは次のとおりです。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$newline"/>
<xsl:text>Tests of the substring() function:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text> substring('Now is the time', 4)="</xsl:text>
<xsl:value-of select="substring('Now is the time', 4)"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> substring('Now is the time', 4, 6)="</xsl:text>
<xsl:value-of select="substring('Now is the time', 4, 6)"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> substring('Now is the time', 4, -6)="</xsl:text>
<xsl:value-of select="substring('Now is the time', 4, -6)"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> substring('Now is the time', -3, 6)="</xsl:text>
<xsl:value-of select="substring('Now is the time', -3, 6)"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> substring('Now is the time', 54, 6)="</xsl:text>
<xsl:value-of select="substring('Now is the time', 54, 6)"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text> count(//*)=</xsl:text>
<xsl:value-of select="count(//*)"/>
<xsl:value-of select="$newline"/>
<xsl:text> substring('Here is a really long string', </xsl:text>
<:xsl:text>count(//*))="</xsl:text>
<xsl:value-of
select="substring('Here is a really long string', count(//*))"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> substring('Here is a less long string', </xsl:text>
<xsl:text>count(//*) mod 7, 7)="</xsl:text>
<xsl:value-of
select="substring('Here is a less long string', count(//*) mod 7, 7)"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> substring(/test/question[1]/text, 3, 7)="</xsl:text>
<xsl:value-of select="substring(//*, 3, 7)"/>
<xsl:text>"</xsl:text>
<xsl:value-of select="$newline"/>
</xsl:template>
</xsl:stylesheet>
Saxon プロセッサを使用した場合の結果は次のとおりです。
Tests of the substring() function:
substring('Now is the time', 4)=" is the time"
substring('Now is the time', 4, 6)=" is th"
substring('Now is the time', 4, -6)="
substring('Now is the time', -3, 6)="No"
substring('Now is the time', 54, 6)="
count(//*)=10
substring('Here is a really long string', count(//*))=" really long string"
substring('Here is a less long string', count(//*) mod 7, 7)="re is a"
substring(/test/question[1]/text, 3, 7)=" This i"
Xalan を使用して同じ変換を実行すると、ランタイムエラーが発生します。
file:///D:/O'Reilly/XSLT/bookSamples/AppendixC/substringfunction.xsl; Line 26;
Column 65;
Tests of the substring() function:
substring('Now is the time', 4)=" is the time"
substring('Now is the time', 4, 6)=" is th"
substring('Now is the time', 4, -6)="
XSLT Error (javax.xml.transform.TransformerException): String index out of range
: -3
現時点では、XT、Saxon、および Oracle の各プロセッサからはすべて正しい結果が得られます。Xalan と Microsoft の XSLT ツールではランタイムの例外が生成されました。ここでは、substring() 関数の適切なパラメータを使用して、実装が異なっても困らないようにすることを目的としています。 |
|