Skip to main content

XSLT <xsl:apply-template> Element

The <xsl:apply-templates> element applies a template rule to the current element or the current element's child nodes.

<xsl:apply-template> Element


The syntax of <xsl:apply-template> is as follows:

mode=QName >


The table describes the meaning of the attributes of <xsl:apply-template>:

selectUsed to process nodes selected by an XPath expression, instead of processing all the children.
modeAllows an element as specified by its Qualified Names to be processed multiple times, each time producing a different result.


Let's see an example:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>

<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<book category="web">
<title lang="en">XQuery Tips</title>
<author>Tom Nolan</author>
<book category="web">
<title lang="en">Learn XML</title>
<author>Tutorial Reference</author>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">

<xsl:template match="/">
<h2>The Bookstore</h2>

<xsl:template match="book">
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="author"/>
<xsl:apply-templates select="price"/>

<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />

<xsl:template match="artist">
Author: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />

<xsl:template match="price">
Price: <span style="color:#0000ff">
<xsl:value-of select="."/></span>
<br />


If we add a "select" attribute to the <xsl:apply-templates> element, it will only process child elements that match the value of the attribute.

We can use the "select" attribute to specify in which order the child nodes should be processed.