xsl:apply-template 記述例
ソースXML文書
<?xml version="1.0" encoding="Shift_JIS" ?> <book> <title>XMLマスターとは</title> <price>2500</price> </book>
XSLTスタイルシート
<?xml version="1.0" encoding="Shift_JIS" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <magazine> <title-price> <xsl:apply-templates select="book" /> </title-price> </magazine> </xsl:template> <xsl:template match="book"> <xsl:value-of select="title" /> <xsl:value-of select="price" /> </xsl:template> </xsl:stylesheet>
結果XML文書
<?xml version="1.0" encoding="UTF-8"?> <magazine><title-price>XMLマスターとは2500</title-price></magazine>
ソースXML文書の正しいノードを指定しなかった場合は、何も処理が行われない。
ソースXML文書
<?xml version="1.0" encoding="Shift_JIS" ?> <book> <title>XMLマスターとは</title> <price>2500</price> </book>
XSLTスタイルシート
<?xml version="1.0" encoding="Shift_JIS" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <magazine> <xsl:apply-templates select="answer" /> </magazine> </xsl:template> <xsl:template match="answer"> <title-price> <xsl:value-of select="title" /> <xsl:value-of select="price" /> </title-price> </xsl:template> </xsl:stylesheet>
結果XML文書
<?xml version="1.0" encoding="UTF-8"?> <magazine/>
選択したノードに対応するテンプレートルールが存在しない場合や、select属性を指定しなかった場合は組込みテンプレートルールが適用される。
組込みテンプレート:
要素ノードとルートノードの組込みテンプレートルール
<xsl:template match="*|/">
<xsl:apply-templates />
</xsl:template>
テキストノードと属性ノードの組込みテンプレートルール
<xsl:template match="text()|@*">
<xsl:value-of select="." />
</xsl:template>
ソースXML文書
<?xml version="1.0" encoding="Shift_JIS" ?> <book> <title>XMLマスターとは</title> <price>2500</price> </book>
XSLTスタイルシート
<?xml version="1.0" encoding="Shift_JIS" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <magazine> <xsl:apply-templates select="book" /> </magazine> </xsl:template> <xsl:template match="answer"> <title-price> <xsl:value-of select="title" /> <xsl:value-of select="price" /> </title-price> </xsl:template> </xsl:stylesheet>
結果XML文書
<?xml version="1.0" encoding="UTF-8"?> <magazine> XMLマスターとは 2500 </magazine>
page revision: 8, last edited: 12 May 2008 18:32