XML:XSLT:インクルード(include):別ファイルインクルードを使って、テンプレートの再利用
XML (18 items)
2004年10月19日
複数のXSLT内で同じ処理を行うような場合、
1つの別のXSLファイルをインクルードファイルとして使用し生産性を高める。
<記述例>
<xsl:include href="include.xsl" />
・XMLデータ(data7.xml)
<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="data7.xsl"?>
<test>
<count>3</count>
<count>5</count>
<count>1</count>
<count>4</count>
<count>2</count>
</test>
・XMLデータを編集するXSLT(data7.xsl)
<?xml version="1.0" encoding="Shift_JIS" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output
method="html"
version="1.0"
encoding="Shift_JIS"
omit-xml-declaration="yes"
standalone="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
indent="yes"
media-type="text/html"/>
<xsl:include href="include.xsl" />
<xsl:template match="test">
<xsl:apply-templates select="count" />
</xsl:template>
<xsl:template match="count">
No.<xsl:number value="position()" format="1"/>=<xsl:value-of select="." /><br />
</xsl:template>
</xsl:stylesheet>
・インクルードXSLT(include.xsl)
※HTMLのHEAD、BODYタグ等をインクルードファイル側で記述。
<?xml version="1.0" encoding="Shift_JIS" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output
method="html"
version="1.0"
encoding="Shift_JIS"
omit-xml-declaration="yes"
standalone="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
indent="yes"
media-type="text/html"/>
<xsl:template match="/">
<!-- HTML出力部 -->
<html>
<head>
<title>Result</title>
</head>
<body>
<xsl:apply-templates />
<!-- HTML出力部 -->
</body>
</html>
</xsl:template>
</xsl:stylesheet>
・生成したHTML
<html>
<head>
<title>Result</title>
</head>
<body>
No.1=3
No.2=5
No.3=1
No.4=4
No.5=2
</body>
</html>
■使用例のダウンロード
ダウンロードし、data7.xmlをブラウザで見る事で確認できます。
>>ダウンロード
※XSLTの動くブラウザでお試し下さい。IEであればIE6以上です。