次の方法で共有


starts-with 関数

1 番目の引数が 2 番目の引数の文字列で始まる場合、true を返します。それ以外の場合、false を返します。

boolean starts-with(string, string)

解説

引数が文字列型でない場合は、string() 関数を使って引数が文字列に変換された後、その変換の結果が評価されます。

ヒント

この関数にノード セットを引数として渡し、文字列変換を行うと、予期しない結果が生じることがあります。詳細については、「string 関数」を参照してください。

この関数では、大文字と小文字が区別されます。

次の例では、starts-with() 関数を使用して、タイトルが大文字の "W" で始まる本のコレクションを照会する方法を示します。

XML ファイル (starts-with.xml)

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

href="contains.xsl"?>      
<bookstore>
  <book>
     <title>The Weather Pattern</title>
     <author>Weather Man</author>
     <price>100.00</price>
  </book>
  <book>
     <title>Weaving Patterns</title>
     <author>Weaver</author>
     <price>150.00</price>
  </book>
  <book>
     <title>Speech Pattern</title>
     <author>Speaker</author>
     <price>15.00</price>
  </book>
  <book>
     <title>Writing Style</title>
     <author>Writer</author>
     <price>1500.00</price>
  </book>
</bookstore>

XSLT ファイル (starts-with.xsl)

<?xml version='1.0'?>
<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="/">
    <html>
       <head><title>example</title></head>
    <body>
       <xsl:apply-templates select="//book"/>
    </body>
    </html>
  </xsl:template>

  <xsl:template match="book">
     <xsl:if test="starts-with(title, 'W')">
       <DIV>
         <B><xsl:value-of select="title"/></B> by 
         <I><xsl:value-of select="author"/></I> costs
         <xsl:value-of select="price"/>.
       </DIV>
     </xsl:if>
  </xsl:template>

</xsl:stylesheet>

出力

この XSLT スタイル シートを XML ファイル (starts-with.xml) に適用すると、次の出力が生成されます。

Weaving Patterns by Weaver costs 150.00.

Writing Style by Writer costs 1500.00.

参照

リファレンス

XML データ型リファレンス