XSLT <apply-template> Element
Last Updated :
16 Oct, 2023
The XSLT Apply Template tag is used to apply the appropriate templates in the context of the selected node. This tag is used to direct the processor to apply templates on node elements of XML.
Syntax:
<xsl:apply-template
select = Expression
mode = Name>
// Other content
</xsl:apply-template>
Attributes:
- select: It is an XPath expression to find the node to select.
- mode: If there are multiple processing ways for a node this parameter is used to distinguish between them.
Example 1: In this example, we will apply templates for different nodes of XML which show values of nodes in different colors. Save the given file as specified and view the xsl file in the web browser.
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
<? xml-stylesheet type = "text/xsl " href = "Rule.xsl" ?>
< student >
< s >
< name > Divyank Singh Sikarwar </ name >
< branch > CSE</ branch >
< age >18</ age >
< city > Agra </ city >
</ s >
< s >
< name > Aniket Chauhan </ name >
< branch > CSE</ branch >
< age > 20</ age >
< city > Shahjahanpur </ city >
</ s >
< s >
< name > Simran Agarwal</ name >
< branch > CSE</ branch >
< age > 23</ age >
< city > Buland Shar</ city >
</ s >
< s >
< name > Abhay Chauhan</ name >
< branch > CSE</ branch >
< age > 17</ age >
< city > Shahjahanpur</ city >
</ s >
< s >
< name > Himanshu Bhatia</ name >
< branch > IT</ branch >
< age > 25</ age >
< city > Indore</ city >
</ s >
</ student >
|
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< xsl:stylesheet version = "1.0"
< xsl:template match = "/" >
< html >
< body >
< h1 align = "center" >Students' Basic Details</ h1 >
< table border = "3" align = "center" >
< tr >
< th >Name</ th >
< th >Branch</ th >
< th >Age</ th >
</ tr >
< xsl:for-each select = "student/s" >
< tr >
< td >
< xsl:apply-templates select = "name" />
</ td >
< td >
< xsl:apply-templates select = "branch" />
</ td >
< td >
< xsl:apply-templates select = "age" />
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ body >
</ html >
</ xsl:template >
< xsl:template match = "name" >
< span style = "color:#ff0000" >
< xsl:value-of select = "." />
</ span >
< br />
</ xsl:template >
< xsl:template match = "branch" >
< span style = "color:#0ff000" >
< xsl:value-of select = "." />
</ span >
< br />
</ xsl:template >
< xsl:template match = "age" >
< span style = "color:#0000ff" >
< xsl:value-of select = "." />
</ span >
< br />
</ xsl:template >
</ xsl:stylesheet >
|
Output:
Example 1: output
Example 2: In this example, we will use print the details of each student in one cell of table. Save the file as specified and view xsl file in web browser.
File Name: Test.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
<? xml-stylesheet type = "text/xsl " href = "Rule.xsl" ?>
< student >
< s >
< name > Divyank Singh Sikarwar </ name >
< branch > CSE</ branch >
< age >18</ age >
< city > Agra </ city >
</ s >
< s >
< name > Aniket Chauhan </ name >
< branch > CSE</ branch >
< age > 20</ age >
< city > Shahjahanpur </ city >
</ s >
< s >
< name > Simran Agarwal</ name >
< branch > CSE</ branch >
< age > 23</ age >
< city > Buland Shar</ city >
</ s >
< s >
< name > Abhay Chauhan</ name >
< branch > CSE</ branch >
< age > 17</ age >
< city > Shahjahanpur</ city >
</ s >
< s >
< name > Himanshu Bhatia</ name >
< branch > IT</ branch >
< age > 25</ age >
< city > Indore</ city >
</ s >
</ student >
|
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< xsl:stylesheet version = "1.0"
< xsl:template match = "/" >
< html >
< body >
< h1 align = "center" >Students' Basic Details</ h1 >
< table border = "3" align = "center" >
< tr >
< th >Details</ th >
</ tr >
< xsl:for-each select = "student/s" >
< tr >
< td >
< xsl:apply-templates select = "name" />
< xsl:apply-templates select = "branch" />
< xsl:apply-templates select = "age" />
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ body >
</ html >
</ xsl:template >
< xsl:template match = "name" >
Name:
< span style = "font-family:cursive;color:#ff0000" >
< xsl:value-of select = "." />
</ span >
< br />
</ xsl:template >
< xsl:template match = "branch" >
Branch:
< span style = "font-family:serif;color:#0ff000" >
< xsl:value-of select = "." />
</ span >
< br />
</ xsl:template >
< xsl:template match = "age" >
Age:
< span style = "font-family:fantsy;color:#0000ff" >
< xsl:value-of select = "." />
</ span >
< br />
</ xsl:template >
</ xsl:stylesheet >
|
Output:
Example 2: output
In conclusion, The <xsl:apply-templates> tag is a fundamental building block in XSLT transformations, allowing you to define how XML data is processed and transformed into various formats. By selecting nodes and applying specific templates, you can achieve precise control over the transformation process, making XSLT a powerful tool for data conversion and presentation.
Share your thoughts in the comments
Please Login to comment...