contains()関数

書式・・・ contains(string1,string2)

・string1に文字列string2が含まれるかどうかを調べ、真偽値(true、false)を返す。
・引数string1に要素ノードや属性ノードを指定してもよい。

[サンプルXML文書を表示](下のフレームに)

XMLソース

<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="contains01.xsl"?>
<root>
<title>KodayanHomepage</title>
<data>
<web>
<title gp="PC005">大阪日本橋DOS/Vプロムナード</title>
<url>http://homepage1.nifty.com/kodayan/dosv/dosv.htm</url>
</web>
<web>
<title gp="PRG237">CSSとDynamicHTML</title>
<url>http://homepage1.nifty.com/kodayan/dhtm/frameset.htm</url>
</web>
<web>
<title gp="PRG108">XMLって?</title>
<url>http://homepage1.nifty.com/kodayan/xml/index.htm</url>
</web>
<web>
<title gp="PRG216">PerlでCGI</title>
<url>http://homepage1.nifty.com/kodayan/cgi/cgifset.htm</url>
</web>
<web>
<title gp="PRG083">Java Script</title>
<url>http://homepage1.nifty.com/kodayan/javasc/index.htm</url>
</web>
<web>
<title gp="OTH205">お気に入りLinks</title>
<url>http://homepage1.nifty.com/kodayan/mylink.htm</url>
</web>
<web>
<title gp="OTH331">自己紹介</title>
<url>http://homepage1.nifty.com/kodayan/etc/etc80.htm</url>
</web>
</data>
<end>以上です(04/4/11)</end>
</root>

XSLソース

<?xml version="1.0" encoding="Shift_Jis"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="Shift_JIS"/>

<xsl:template match="/">
<center><H3><xsl:value-of select="root/title"/></H3></center>
<xsl:apply-templates select="root/data/web"/>
</xsl:template>

<xsl:template match="web">
<!--title要素のgr属性に文字列'PRG'を含む場合だけ処理をする-->
<xsl:if test="title[contains(@gp,'PRG')]">
<xsl:value-of select="title" /><br/>
<!--title要素に文字列'XML'が含まれる場合"true"を、含まれない場合は"false"を返す-->
<xsl:value-of select="contains(title,'XML')" /><br/>
<!--文字列'index.htm'が含まれるurl要素だけテンプレートを適用する-->
<xsl:apply-templates select="url[contains(.,'index.htm')]"/>
<!--文字列'index.htm'を含んでいないurl要素だけその内容を表示する-->
<xsl:value-of select="url[not(contains(.,'index.htm'))]" /><br/>
<hr/>
</xsl:if>
</xsl:template>
<xsl:template match="url">
ファイル名は「index.htm」です
</xsl:template>
</xsl:stylesheet>

end(04/4/11)