XSLT <xsl:apply-templates>Element


XSLT 元素参考手册Complete XSLT Element Reference Manual

Definition and Usage

The <xsl:apply-templates> element can apply templates to the current element or to the child nodes of the current element.

If we add a select attribute to the <xsl:apply-templates> element, it will only process child elements that match the value of that attribute. We can use the select attribute to specify the order of the child nodes to be processed.


Syntax

<xsl:apply-templates select="expression" mode="name">

<!-- Content:(xsl:sort|xsl:with-param)* -->

</xsl:apply-templates>

Attributes

Attribute Value Description
select expression Optional. Specifies the nodes to be processed. The asterisk selects the entire node set. If this attribute is omitted, all child nodes of the current node will be selected.
mode name Optional. If multiple processing templates are defined for the same element, the mode attribute can be used to differentiate them.

Example 1

Wrap each title element in the document with h1 elements:

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

Example 2

Wrap all title elements in the document that are child elements of message with h1 elements:

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

Example 3

Wrap all child nodes of message with the mode attribute set to "big" in the document with h1 elements:

<?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 元素参考手册Complete XSLT Element Reference Manual Other Extensions