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>
<!-- 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>
<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>
<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>
<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元素參考手冊