XSLT Element

We can apply a template rule to the current element or to the current element’s child nodes, using the <xsl:apply-templates> element.

The <xsl:apply-templates> Element:

A template is applied by the <xsl:apply-templates> element to the current element or to the current element’s child nodes. To process only the child elements that match a particular value, a “select” attribute can be added to the <xsl:apply-templates> element. The order in which the child nodes are to be processed can also be specified using the “select” attribute.

Example:

Books.xml:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 
  <book category="Child">
    <title lang="en">ABC</title>
    <author>Author Name</author>
    <year>2020</year>
    <price>100.00</price>
  </book>
 
  <book category="IT">
    <title lang="en">XQuery Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
 <year>2005</year>
    <price>300.00</price>
  </book>
 
  <book category="Sociology">
    <title lang="en">Sociology 1</title>
    <author>Author Name</author>
 <year>2010</year>
    <price>250.00</price>
  </book>
 
  <book category="GK">
    <title lang="en">Current Affairs</title>
    <author>Author Name</author>
 <year>2004</year>
    <price>500.00</price>
  </book>
 
  <book category="Science">
    <title lang="en">Science Book</title>
    <author>Author 1</author>
    <author>Author 2</author>
    <author>Author 3</author>
 <year>2011</year>
    <price>150.00</price>
  </book>
 
</bookstore>

XSLT Code:

<?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>List of Books</h2>  
  <xsl:apply-templates/>  
  </body>
  </html>
</xsl:template>
 
<xsl:template match="book">
  <p>
    <xsl:apply-templates select="title"/>  
    <xsl:apply-templates select="price"/>
  </p>
</xsl:template>
 
<xsl:template match="title">
  Title: <span style="color:#008080">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>
 
<xsl:template match="price">
  Price: <span style="color:#00FFFF">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>
 
</xsl:stylesheet>

Output:

 

Please Share