Updated April 10, 2023
Definition of XSLT apply-templates
XSLT apply-templates define how to find elements and help in removing unwanted text in the document. It applies a template rule to the current child nodes. It adds a select attribute along with the template to specify the order of child nodes to process the current task with the help of the XSLT processor. The targeted elements are sorted inside these apply-templates. This is also a mechanism to do iteration over a list of nodes.
Syntax:
The Syntax declaration of the above element is given as
<xsl:apply-template
select = Expression
mode = XXname >
</xsl:apply-template>
The above syntax has three attributes namely mode, name, and priority. The name attribute calls the functions, mode neglects the candidate templates and priority defines the specialty or the preferences.
How apply templates work in XSLT?
<xsl: apply-template/> is placed within the template for the root node. It assures where the content of its children appears on the transformed document. Templates follow certain priorities when targeting items with any one mode. xsl: apply-templates can carry only <xsl: sort> and< xsl: with-param> elements It includes precedence, highest priority defined on the templates, and finally imposing a set of rules to match the target nodes. And if the priority is the same then the last match is encountered. Since the template node is not returned, they automatically calculate how the node is displayed in the output.
When taking very large documents performance improvement is very low in the case of Xpath expression by traversing an arbitrary location in a tree. In such a situation using apply-templates is much flexible. Rather than filtering the process node to meet particular criteria, it is best to select the nodes. If suppose there is an expression hello, the instruction that iterates over the node-set is given as
<xsl: apply-templates select=” hello”/>
For each node here in the select statement, the processor invokes the correct templates rule.
The default select value is child: node () descendants. This concatenates the occurred sequence values. The apply template helps in processing the order for instance let’s take if we want to list the place of the people in the source document and we happened to place the address first, regardless of the order in which the input document occurs. It is required to name a template.
Let’s take a sample source XML
<Trains>
<Train id=”9876”>
<pnr>p8777</pnr>
<trainname> XXX express </trainname>
</Train>
……
</Trains>
Matching through XSL includes
<h3> List of Train </h3>
<ul class =” Train-List”>
<xsl: apply-templates select =” Trains/Train”/>
</ul>
<xsl:template match =”Train”>
<xsl:value-of select>
</xsl:template>
When the apply-templates are invoked, the processor examines the original style sheet template rules to check for the patterns. And it does the recursion call until it finds the non-empty list of nodes to continue. The select attribute is sometimes made optional in such case it is defined as
<xsl: apply-templates select="node ()"/>
Examples
Let us discuss examples of XSLT apply-templates.
Example #1
XML
<?xml version="1.0" encoding="UTF-8"?>
<articles>
<article id="x34675">
<name>Apache Spark Architecture</name>
<month>december</month>
<author>kay vennisla </author>
<feedback > high rating</feedback>
</article>
<article id="g6754">
<name>Vue Js Architecture</name>
<month>January</month>
<author>Britt kay </author>
<feedback> medium rating</feedback>
</article>
<article id="k9054">
<name>React for-Loop</name>
<month>March</month>
<author> Danny Jack </author>
<feedback> High rating</feedback>
</article>
</articles>
XSL
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>BLOGS- EDUCBA</h2>
<xsl:apply-templates select = "articles/article" />
</body>
</html>
</xsl:template>
<xsl:template match = "articles/article">
<xsl:apply-templates select = "@id" />
<xsl:apply-templates select = "name" />
<xsl:apply-templates select = "month" />
<xsl:apply-templates select = "author" />
<xsl:apply-templates select = "feedback" />
<br />
</xsl:template>
<xsl:template match = "@id">
<span style = "font-size = 26px;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "name">
First Name:<span style = "color:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "month">
Last Name:<span style = "color:blue;">
<xsl:value-of select = "." />
</span>
<br />
<</xsl:template>
<xsl:template match = "author">
Nick Name:<span style = "color:orange;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "feedback">
Marks:<span style = "color:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>
Explanation
In the above code, we had mentioned the main template match along with that we addressed the <xsl: apply-templates> element for the child nodes too to create an output from the several data.
Output:
Example #2
XML
<?xml version="1.0" encoding="UTF-8"?>
<PlayersList>
<Game type="Gymnastics">
<name>Dipa Karmakar</name>
<Members>Captain</Members>
<place>India</place>
</Game>
<Game type="NetBall">
<name>Marai Tutaia</name>
<Members>Goal Shooter</Members>
<place>New Zealand</place>
</Game>
<Game type="Squash">
<name>Saurav Ghosal</name>
<Members>leader</Members>
<place>Asia</place>
</Game>
<Game type="Cricket">
<name>Vishwa Fernando</name>
<Members>Batsman</Members>
<place>SriLanka</place>
</Game>
<Game type="Hockey">
<name>Sidney Crosby</name>
<Members>Over lap</Members>
<place>Pittsburgh</place>
</Game>
</PlayersList>
XSL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="PlayersList/Game">
<html>
<body>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="Members" />
<xsl:apply-templates select="place" />
<br />
</body>
</html>
</xsl:template>
<xsl:template match="name">
<span style="font-size=20px;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
<xsl:template match="Members">
Members:<span style="color:blue;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
<xsl:template match="place">
Place:<span style="color:green;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>
Explanation
The above source code does the apply-templates individually to three nodes and the output is shown as:
Output:
Example #3
XML
<?xml version="1.0" encoding="UTF-8"?>
<patents>
<patent id="154585">
<name>Joe Ancy</name>
<dob>22</dob>
<major>Computers</major>
<results>
<result course="Cplus 101" grade="C-"/>
<result course="Java 101" grade="C+"/>
<result course="Deep Learning 101" grade="D"/>
<result course= "AI 101 " grade= "D" />
</results>
</patent>
<patent id="16589">
<name>Venu RAj</name>
<dob>25</dob>
<major>Maths</major>
<results>
<result course="Regression 101" grade="A"/>
<result course="Xpath 101" grade="A-"/>
<result course="Java 102" grade="A"/>
</results>
</patent>
</patents>
XSLT
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match ="patents">
<Scheduled>
<xsl:apply-templates select="patent">
<xsl:sort select ="dob" data-type=" number"
order="descending"/>
<xsl:sort select="name"/>
</xsl:apply-templates>
</Scheduled>
</xsl:template>
<xsl:template match="patent">
<student name="{name}" dob="{dob}"/>
</xsl:template>
</xsl:stylesheet>
Explanation
The select attribute here identifies the Xpath of the nodes to process the context node. In the above code, the apply template uses the sort attribute to sort the specific node name and dob. The children of class in our code are patents and patent and the result is given as.
Output:
Advantages
- The other XSL elements retrieve data from the child elements.
- Has the advantage of instructing the XSLT processor to apply correct templates for the corresponding node-set.
- They are the recursive Application that traverses down the XML tree.
Conclusion
Therefore, XSLT being a domain-specific language contains few template rules where it starts at the root node of the Source document. In this article, we have seen how the template rule is applied to the source XML to extract the preferred nodes.
Recommended Articles
This is a guide to XSLT apply-templates. Here we discuss definition, syntax, and parameters, How apply-templates work in XSLT? examples with code implementation. You may also have a look at the following articles to learn more –