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