テンプレートの優先度

対応するテンプレート定義内容<xsl:template >が複数ある場合の対処方法。

・要素名だけよりロケーションステップの区切りで指定された要素名のほうが優先度は高い。

・priority属性で優先度を設定・・・数値が大きくなるほど優先度が高い。

・同等の優先度の場合,一番最後に記述されているものが適応される。

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

XMLソース

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="temp03.xsl" ?>
<root>
<title>テンプレートの優先度<sub>テスト</sub></title>
<contents>
<ken>鳥取県</ken>
<city>倉吉市</city>
<hot>
 <yama>大山</yama>
</hot>
</contents>
<end>以上です(03/10/15)</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/contents"/>
</xsl:template>

<!-- 対応するテンプレート定義内容<xsl:template >が複数ある場合、一番最後に
記述されているものが適応される
-->
<xsl:template match="contents/ken">
<H4 style="color:blue"><xsl:value-of select="."/></H4>
</xsl:template>
<xsl:template match="contents/ken">
<H4 style="color:red"><xsl:value-of select="."/></H4>
<HR/>
</xsl:template>
<!--priority属性で優先度を設定(数値が大きくなるほど優先度が高い)-->
<xsl:template match="contents/city">
<H4 style="color:blue"><xsl:value-of select="."/></H4>
</xsl:template>
<xsl:template match="contents/city" priority="3">
<H4 style="color:red"><xsl:value-of select="."/></H4>
<HR/>
</xsl:template>
<xsl:template match="contents/city" priority="1">
<H4 style="color:green"><xsl:value-of select="."/></H4>
</xsl:template>
<xsl:template match="contents/city">
<H4 style="color:black"><xsl:value-of select="."/></H4>
</xsl:template>
<!--要素名だけよりロケーションステップの区切りで指定された要素名のほうが優先度は高い-->
<xsl:template match="hot/yama">
<H4 style="color:blue"><xsl:value-of select="."/></H4>
</xsl:template>
<xsl:template match="yama">
<H4 style="color:green"><xsl:value-of select="."/></H4>
</xsl:template>
</xsl:stylesheet>

end(03/10/15)