<xsl:include>の概要
この要素は、外部からスタイルシートを読み込むことができる。
href属性に、ファイルパスを記述する。
サンプルコード
xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="sample.xsl"?> <root> <product> <name>ゲームソフト</name> <price>5000</price> </product> <product> <name>充電器</name> <price>1000</price> </product> </root> |
XSLT
メインのXSLT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:for-each select="root/product"> <商品> <xsl:apply-templates select="name"/> <xsl:apply-templates select="price"/> </商品> </xsl:for-each> </xsl:template> <xsl:include href="sample2.xsl"/> </xsl:stylesheet> |
外部のXSLT
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="name"> <商品名><xsl:value-of select="." /></商品名> </xsl:template> <xsl:template match="price"> <価格><xsl:value-of select="." /></価格> </xsl:template> </xsl:stylesheet> |
変換結果
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="UTF-8"?> <商品> <商品名>ゲームソフト</商品名> <価格>5000</価格> </商品> <商品> <商品名>充電器</商品名> <価格>1000</価格> </商品> |
解説
<xsl:include href=”sample2.xsl”/>で外部のXSLTを読み込んでいる。
その結果、外部で宣言しているテンプレートをapply-templatesで呼びだせるようになる。
このように、処理をファイルごとに細かく分けたい場合に使用できる。