<xsl:template match=“student"> Found a learner!
Found a learner! Found a learner! Found a learner! Mr. Bean
How it Works? <?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> <?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> Step 1: For all matching student tags, display output Found a learner! Step 2: Come here and run the template recursively for all the student tags, displaying Found a learner! Step 3: For all tags other than student , there is no template in our XSL. Therefore, blindly output them, as they are!
Found a learner! Found a learner! Found a learner!
The XSLT processor begins at the root element when looking for template matches. It finds a match for the root element class .
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 .
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.
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!"
Most transformation in XSLT is driven by two elements, <xsl:template> and <xsl:apply-templates>.
Processing begins at root, and then:
For each X in the current node, processor searches for all <xsl:template match = “pattern”> elements in the stylesheet that potentially match the node.
The selected <xsl:template match = “pattern”> is instantiated using node X as its current node. This templates typically copies data from the source XML, or produces brand new content in combination with the source data.
If the template contains <xsl:apply-templates select = “newPattern” />, a new current node is created and the process repeats recursively.
Comparing <xsl:template> to <xsl:apply-template>
Think about the former as similar to a Java method definition, and the later as invoking that method