XSLT <xsl:apply-templates> エレメント

定義と使用法

<xsl:apply-templates> 要素は現在の要素または現在の要素の子要素にテンプレートを適用できます。

もし <xsl:apply-templates> 要素に select 属性を追加すると、その属性の値に一致する子要素のみが処理されます。select 属性を使用して子要素の処理順序を指定できます。

文法

<xsl:apply-templates select="expression" mode="name">
  <!-- Content:(xsl:sort|xsl:with-param)* -->
</xsl:apply-templates>

属性

属性 説明
select 表現 オプション。処理するノードを指定します。星号はすべてのノード集合を選択します。この属性を省略すると、現在のノードのすべての子ノードが選択されます。
mode 名前 オプション。同じ要素に対して複数の処理方法が定義されている場合、mode を使用してそれらを区別することができます。

例 1

ドキュメント内の各 title 要素を h1 要素で囲む:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="title">
  <h1><xsl:apply-templates/></h1>
</xsl:template>
</xsl:stylesheet>

例 2

ドキュメント内のすべての message に属する子要素の title 要素を h1 要素で囲む:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="message">
  <h1><xsl:apply-templates select="title"/></h1>
</xsl:template>
</xsl:stylesheet>

例 3

ドキュメント内の mode 属性が "big" に設定された message のすべての子ノードを h1 要素で囲む:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="message">
  <h1><xsl:apply-templates select="*" mode="big"/></h1>
</xsl:template>
</xsl:stylesheet>