XSLT - テンプレートの継承(xsl:apply-imports)Advertisement書式
変数の宣言。
<xsl:apply-imports /> 解説
xsl:import を使用すると、スタイルシートに優先度が設定され、その優先度が低い方のテンプレートは無視されてしまう。以下、例を示す。
sample.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="./style.xsl"?>
<document>
<book id="10">
<title>やさしいXML入門<sub>基礎から応用まで</sub></title>
<price>2000</price>
</book>
</document>
style.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="./imp.xsl" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="title">
<i><xsl:value-of select="."/></i>
</xsl:template>
</xsl:stylesheet>
imp.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="sub">
<b><xsl:value-of select="."/></b>
</xsl:template>
</xsl:stylesheet>
適用結果
<i>やさしいXML入門基礎から応用まで</i>「基礎から応用まで」の部分にimportしてきたスタイルシートの<b>属性が適用されていない。これを適用させるために <xsl:apply-imports> 要素を使用する。 スタイルの継承
sample.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<?xml-stylesheet type="text/xsl" href="./style.xsl"?>
<document>
<book id="10">
<title>やさしいXML入門<sub>基礎から応用まで</sub></title>
<price>2000</price>
</book>
</document>
style.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="./imp.xsl" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="title">
<i><xsl:apply-imports /></i>
</xsl:template>
</xsl:stylesheet>
imp.xsl
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="sub">
<b><xsl:value-of select="."/></b>
</xsl:template>
</xsl:stylesheet>
スタイルシート適用結果
<i>やさしいXML入門<b>基礎から応用まで</b></i>優先順位が低い方のテンプレートが打ち消されることなく、適用された。 Advertisement |
ショートカット・634・634ブログ ・このカテゴリのトップページに戻る ・Incubator(Pukiwiki) ・634ラボ UIコレクションギャラリー ZO-3ジェネレーター サイト検索Y!ログール |