Updated April 4, 2023
Introduction to XSLT match
The following article provides an outline for the XSLT match. The match is an attribute that gives hands to the template for the association to an XML element. And it defines the whole document by implying with the symbol “/”. When it matches an XML node, the template is invoked by the processor. <xsl: template match> matches the root node of the source document. If it doesn’t find the match nodes, default rules are applied.
Syntax:
The general syntax is given as:
<xsl:template match =”/”>
//apply , value-of </xsl:template>
The match follows an expression of Xpath.
How match Work in XSLT?
XSL has a collection of templates with certain conditions, which produces some control over the transformation. Templates do three main things. The first thing is matching a class of nodes which holds an XSLT pattern. If the templates match any of the nodes, then the processor picks one. The second is a priority, and the last is to specify the structure. The <xsl: template match=”/”> is an unnamed template and make use of apply-templates to specify the selected node of the input document. It is made mandatory unless it has a name attribute. In layman terms, we can say that an attribute is a pattern that defines which nodes to have and what attributes to apply to them. The match attribute includes a pattern expression, and it is used in a different process.
Few patterns are listed below:
Let’s take a sample XML document.
<product>
<items id=”90” />
<product>
So now the patterns match for the above node element is tabulated as:
1 | Match=” product” | This matches a product element of the source document. |
2 | Match=” items/product.” | This match <product> element which are children of <items> element. |
3 | Match=” items//product” | This matches the elements contained within the item’s element. |
4 | Match=” items/product[@id]” | Matches <product> elements that are children of <items> elements and that have an ‘id’ attribute. |
The match keyword makes use of a pattern in an XSLT stylesheet, and it is an Xpath expression where the matching is made with the current node and also recursively traverse down the node in the tree to match additional templates.
Next, we shall move with certain rules to allow recursive processing.
1. To process from the root node, the template match rule is given as.
<xsl:template match =”/”>
//apply
</xsl:template>
2. To traverse every element in the tree.
<xsl:template match =”*”>
//apply
</xsl:template>
3. With attributes.
<xsl:template match =”@ *”>
//apply </xsl:template>
4. To include the value of select for the text data to display.
<xsl:template match =”text()”>
<xsl:value-of select =”.”/>
</xsl:template>
All these templates are applied to the nodes of the input document, and when matches are made, the templates give out the desired output.
Examples of XSLT match
Given below are the examples of XSLT match:
The following example uses xsl instruction, and the basic element and logic result by using the templates is given in the output.
Example #1
Code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<hello>
<converse xml:lang="en">Hello</converse>
<converse xml:lang="fr">hellodam</converse>
<converse xml:lang="es">Gutan Dag</converse>
<converse xml:lang="de">okhklsjhd</converse>
</hello>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="hello">
<xsl:apply-templates select="converse[lang('es')]" />
</xsl:template>
<xsl:template match="converse[lang('es')]">
<xsl:text>Norwegian: </xsl:text>
<xsl:value-of select="." />
</xsl:template></xsl:stylesheet>
Explanation:
- The above code matches the element by an attribute value where the match selects the lang value, and the output of ‘es’ matching the respective template is given.
Output:
Example #2
Code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Unit>
<Product deliverDate="02/21/1990" approval="yes">
<lastname>kkk</lastname>
<firstname>boby</firstname>
<Saleprice>50000</Saleprice>
</Product>
<Product deliverDate="01/11/1910" approval="no">
<lastname>Smith</lastname>
<firstname>Rebacca</firstname>
<Saleprice>65000</Saleprice>
</Product>
<Product deliverDate="08/16/2000" approval="yes">
<lastname>Blikisht</lastname>
<firstname>boby</firstname>
<Saleprice>85000</Saleprice>
</Product>
</Unit>
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="Product[@deliverDate='01/11/1910']" />
</xsl:stylesheet>
Explanation:
- The above code uses match element with certain attributes. Here we insist on deliverdate where the XML document matches the fields, and the output is given.
Output:
Example #3
Code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Tramp>
<Product deliverDate="02/21/1990" approval="yes">
<lastname>kkk</lastname>
<firstname>boby</firstname>
<Saleprice>50000</Saleprice>
<tax>
<gst> 1.2</gst>
<others> 14</others>
</tax>
</Product>
<Product deliverDate="01/11/1910" approval="no">
<lastname>Smith</lastname>
<firstname>Rebacca</firstname>
<Saleprice>65000</Saleprice>
<tax>
<gst> 2.2</gst>
<others> 16</others>
</tax>
</Product>
<Product deliverDate="08/16/2000" approval="yes">
<lastname>Blikisht</lastname>
<firstname>boby</firstname>
<Saleprice>85000</Saleprice>
</Product>
</Tramp>
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="*|/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()|@*">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="processing-instruction()|comment()" />
</xsl:stylesheet>
Explanation:
- In our source document, we had attributes deliver date that indicates the type of text to be displayed, and the last template match uses default value processing comment.
Output:
Example #4
Code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Bigstore>
<shop List="Grocery">
<Cereals lang="en">Toor Dhal</Cereals>
<Brand>Sree Gold</Brand>
<year>2001</year>
<Quantity>150.00</Quantity>
</shop>
<shop List="Grocery">
<Shampoo lang="en">Loreal</Shampoo>
<Brand>USA</Brand>
<year>2005</year>
<Quantity>250.00</Quantity>
</shop>
<shop List="Grocery">
<Soaps lang="en">Lux</Soaps>
<Brand>India</Brand>
<year>2001</year>
<Quantity>210.00</Quantity>
</shop>
</Bigstore>
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>My Store Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Bigstore">
<p>
<xsl:apply-templates select="shop"/>
<xsl:apply-templates select="Cereals"/>
</p>
</xsl:template>
<xsl:template match="shop">
shop: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="Cereals">
Cereals: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
Explanation:
- The above stylesheet transformations are given below, and the output includes the node shops by breaking the contents apart. Also, notice the select attribute embeds the element as we use (.), which indicates to display the contents of the current node. The output then displays an entry for each shop element.
Output:
Example #5
Code:
XML:
<?xml version="1.0"?>
<Parent>
<AA>
<BB>
<CC>
<DD>EDUCBA</DD>
<EE>
<FF>fff</FF>
</EE>
</CC>
<DD>EDUCBA2</DD>
</BB>
</AA>
<AA>
<BB>
<DD>EDUCBA3</DD>
</BB>
</AA>
<DD>EDUCBa4</DD>
</Parent>
XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="/Parent">
<Parent>
<xsl:apply-templates select="*[name(.)!='DD']"/>
<Result>
<xsl:apply-templates select="//DD"/>
</Result>
</Parent>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name(.)}">
<xsl:value-of select="text()"/>
<xsl:apply-templates select="*[name(.)!='DD']"/>
</xsl:element>
</xsl:template>
<xsl:template match="DD">
<Result>
<xsl:value-of select="text()"/>
</Result>
</xsl:template>
</xsl:stylesheet>
Explanation:
- The above code takes one or two elements with the specific name and gets wraps in a certain name, and the rest of the element remains the same. Here we did with the element <DD> where all the elements are grouped in the output in the name <result>.
Output:
Advantages of XSLT match
Given below are the advantages mentioned:
- The match attribute specifies when the templates are to be used. They are the root template that matches the XML document entirely.
- It defines a set of rules to make the stylesheet simple.
Conclusion
XSLT examines the input document in a depth-first traversal manner to find the best match from all the templates. Therefore, in this article, we have seen how match patterns work in XSL transformation with certain rules.
Recommended Articles
This is a guide to the XSLT match. Here we discuss the introduction, how match work in XSLT? Examples and advantages, respectively. You may also have a look at the following articles to learn more –