XSLT <xsl:apply-templates> Element
Elective Courses
Course Recommendation:
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 in which sub-items are processed. Syntax <xsl:apply-templates select="expression" mode="name">
<!-- Content: (xsl:sort|xsl:with-param)* -->
<!-- Content: (xsl:sort|xsl:with-param)* --> | </xsl:apply-templates> | Attribute |
---|---|---|
Value | Description | 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 in an 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 all title elements of all child elements belonging to the message element in the document 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 in the document 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>