<xsl:apply-imports> の例 1
次の例では、<xsl:apply-imports> を使ってコードの再利用の効率を上げる方法を説明します。 例では、次の 4 つのメイン ファイルを使用します。
ops.xml という XML ソース ファイル。 このデータ ファイルでは、add (+)、sub (-)、および mul (*) の 3 つの操作が定義されます。
ops.xsl というメインの XSLT スタイル シート。 このファイルには、2 つの <xsl:import> 要素を含む、操作のテンプレート規則が含まれます。 インポートされるスタイル シートは、指定されたデータ ソースで算術演算と文字列操作を実行します。
arith.xsl というインポートされるスタイル シート。 この XSLT ファイルは、個々の <op> 要素で算術演算を実行します。
str.xsl という、別のインポートされるスタイル シート。 この XSLT ファイルは、独自の文字列操作を実行します。 ここでは、add (+) を文字列の連結に使用します。たとえば、1+2 は 12 になります。 同様に、mul (*) を逆方向の連結に使用します。1*2 は 21 になります。 sub (-) は未定義の文字列操作であることに注意してください。
XML ファイル (ops.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ops.xsl"?>
<ops>
<desc>Some binary operations</desc>
<op name="add" symbol="+">
<operand>1</operand>
<operand>2</operand>
</op>
<op name="sub" symbol="-">
<operand>1</operand>
<operand>2</operand>
</op>
<op name="mul" symbol="*">
<operand>1</operand>
<operand>2</operand>
</op>
</ops>
メインの XSLT ファイル (ops.xsl)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:import href="arith.xsl"/>
<xsl:import href="str.xsl"/>
<xsl:template match="op">
<xsl:value-of select="operand[1]"/>
<xsl:value-of select="@symbol"/>
<xsl:value-of select="operand[2]"/>
= <xsl:apply-imports/>
<br/>
</xsl:template>
</xsl:stylesheet>
インポートされる XSLT ファイル (arith.xsl)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="op[@symbol='+']">
<xsl:value-of select="sum(operand)"/> (from arith.xsl)
</xsl:template>
<xsl:template match="op[@symbol='-']">
<xsl:value-of select="number(operand[1])-number(operand[2])"/>
(from arith.xsl)
</xsl:template>
<xsl:template match="op[@symbol='*']">
<xsl:value-of select="number(operand[1])*number(operand[2])"/>
(from arith.xsl)
</xsl:template>
</xsl:stylesheet>
インポートされる XSLT ファイル (str.xsl)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="desc">
<DIV><xsl:value-of select="."/></DIV>
</xsl:template>
<xsl:template match="op[@name='add']">
<xsl:value-of select="operand[1]"/>
<xsl:value-of select="operand[2]"/> (from str.xsl)
</xsl:template>
<xsl:template match="op[@name='mul']">
<xsl:value-of select="operand[2]"/>
<xsl:value-of select="operand[1]"/> (from str.xsl)
</xsl:template>
</xsl:stylesheet>
出力
次のような出力になります。
Some binary operations
1+2 = 12 (from str.xsl)
1-2 = -1 (from arith.xsl)
1*2 = 21 (from str.xsl)
解説
最後にインポートされたスタイル シートのインポート優先順が最も高くなります。 この例では、str.xsl が最後にインポートされるので、arith.xsl よりインポート優先順位が高くなります。 インポートされたスタイル シートは両方とも、add 操作と mul 操作用のテンプレートを持っています。 str.xsl のテンプレートのみが呼び出されます。 ただし、str.xsl では sub 操作が定義されていないため、arith.xsl で定義されている sub 操作が使用されます。 次のように、メイン XSLT ファイル内で <xsl:import> 要素の順番を入れ替えるとします。
<xsl:import href="str.xsl"/>
<xsl:import href="arith.xsl"/>
その場合、出力は次のようになります。
Some binary operations
1+2 = 3 (from arith.xsl)
1-2 = -1 (from arith.xsl)
1*2 = 2 (from arith.xsl)
また、メインの XSLT ファイル (ops.xsl) 内の <op> 用のオーバーライドする側のテンプレート規則に <xsl:apply-imports/> 命令がない場合、出力は次のようになります。
Some binary operations
1+2 =
1-2 =
1*2 =
つまり、インポートする側のスタイル シート内のテンプレート規則は、インポートされるスタイル シート内の関連するテンプレート規則をオーバーライドします。 <xsl:apply-imports/> 命令を使用すると、これらのオーバーライドされたテンプレート規則を別の方法で再びアクティブにできます。