Bookmark and Share

XSLT - xsl:apply-templates

In the xsl:template lesson, you learned how to find elements and insert text with XSLT. However, you still don't know how to filter out unwanted XML data. This lesson will teach you how to use the xsl:apply-templates element to be more selective of your XML data.

Advertise on Tizag.com

We will continue to use the class.xml file, which is shown below.

XML Code:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<class>
	<student>Jack</student>
	<student>Harry</student>
	<student>Rebecca</student>
	<teacher>Mr. Bean</teacher>
</class>

XSLT - Remove Unwanted Text

Our XSLT code in the xsl:template was supposed to output the message, "Found a learner!" for each student element found. However, the following XSLT code did that and output "Mr. Bean".

XSLT Code (Work in Progress!):

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="student">
		Found a learner!
	</xsl:template>
</xsl:stylesheet>

XSLT Output:

Found a learner! Found a learner! Found a learner! Mr. Bean

By making use of the select attribute and the xsl:apply-templates element, we can eliminate the unwanted text, "Mr. Bean".

  • select attribute: lets you choose specific child elements
  • xsl:apply-templates: lets you decide when and where your xsl:template elements are used

XSLT - Remove Unwanted Children

We are going to have to add some additional code to our XSLT to eliminate this unwanted text. If we were to first match the root element class, we could use the select attribute to select specific child elements.

To do this, we need a new xsl:template that matches our XML document's root element, class. We can then pick the child student using the select attribute. Here's the XSLT code to get the job done.

XSLT Code (Work in Progress!):

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="class">
    <xsl:apply-templates select="student"/>
</xsl:template>
<xsl:template match="student">
    Found a learner!
</xsl:template>
</xsl:stylesheet>

Our XML document with an updated xml-stylesheet link can be seen here (Internet Explorer 6.0+): class2.xml

XSLT Output:

Found a learner! Found a learner! Found a learner!

Success! Now, let's step through the XSLT code to figure out what's really going on.

xsl:template match="class"

The XSLT processor begins at the root element when looking for template matches. Because we have a match for the root element, class, the code we just added is used first.

xsl:apply-templates

In our template that matched class, we use xsl:apply-templates which will check for template matches on all the children of class. The children of class in our XML document are student and teacher.

xsl:apply-templates select="student"

To have the teacher element, "Mr. Bean," ignored, we use the select attribute of xsl:apply-templates to specify only student children.

The XSLT processor then goes searching templates that only match student elements.

xsl:template match="student"

The processor finds the only other template in our XSLT, which prints out, "Found a learner!" for each student element in the XML document. XSLT finds three students, so "Found a learner!" is displayed three times.

XSLT then finishes its processing, and we are left with XSLT output that has eliminated the unwanted text, "Mr. Bean!"

Bookmark and Share




Download Tizag.com's XML Book

If you would rather download the PDF of this tutorial, check out our XML eBook from the Tizag.com store.

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!

Advertise Here

More Tutorials!
Microsoft Office Tutorials Artist Tutorials