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>