XSLT <xsl:apply-templates> 元素


XSLT 元素參考手冊 完整的 XSLT 元素參考手冊

定義和用法

<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 expression 可選。規定要處理的節點。星號選取整個節點集。如果省略該屬性,則將選取當前節點的所有子節點。
mode name 可選。如果存在為相同元素定義的多個處理方法,那麼用 mode 可以區分它們。

實例 1

用 h1 元素包圍文檔中每個 title 元素:

<?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

用 h1 元素包圍文檔中所有屬於 message 的子元素的 title 元素:

<?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

用 h1 元素包圍文檔中 mode 屬性設置為 "big" 的 message 所有子節點:

<?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>


XSLT 元素參考手冊 完整的 XSLT 元素參考手冊