XSLT <xsl:apply-templates> Element
Definition and Usage
The <xsl:apply-templates> element can apply templates to the current element or its child elements.
If we add a select attribute to the <xsl:apply-templates> element, it will only process the child elements that match the value of the attribute. We can use the select attribute to specify the order of processing sub-fragments.
Syntax
<xsl:apply-templates select="expression" mode="name"> <!-- Content:(xsl:sort|xsl:with-param)* --> </xsl:apply-templates>
Attribute
Attribute | Value | Description |
---|---|---|
select | Expression | Optional. Specifies the node to be processed. An 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 methods are defined for the same element, mode can be used to distinguish them. |
Instance
Example 1
Enclose each title element using the h1 element:
<?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
Enclose the title elements of all child elements belonging to message using the h1 element:
<?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
Enclose all child nodes of the message element with mode attribute set to "big" using the h1 element:
<?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>