XSLT <xsl:apply-templates> 元素
完整的 XSLT 元素參考手冊
定義和用法
<xsl:apply-templates> 元素可向當(dāng)前元素或當(dāng)前元素的子節(jié)點應(yīng)用模板。
如果我們向 <xsl:apply-templates> 元素添加 select 屬性,那么它僅會處理匹配該屬性的值的子元素。我們可使用 select 屬性來規(guī)定要處理的子節(jié)點的順序。
語法
<xsl:apply-templates select="expression" mode="name">
<!-- Content:(xsl:sort|xsl:with-param)* -->
</xsl:apply-templates>
屬性
屬性 | 值 | 描述 |
select | expression | 可選。規(guī)定要處理的節(jié)點。星號選取整個節(jié)點集。如果省略該屬性,則將選取當(dāng)前節(jié)點的所有子節(jié)點。 |
mode | name | 可選。如果存在為相同元素定義的多個處理方法,那么用 mode 可以區(qū)分它們。 |
實例 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 屬性設(shè)置為 "big" 的 message 所有子節(jié)點:
<?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 元素參考手冊
更多建議: