EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials XML Tutorial XSLT Template
 

XSLT Template

Updated April 7, 2023

XSLT Template

 

 

Definition of XSLT Template

XSLT Template defines a way to set rules to generate the desired Output in a proper structure for a particular context and to build a template. The template tells the XSLT processor how to format a particular output from the XML source document. The perfect determination is made to the nodes in the node-set with the pattern matching within the XSL template.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

The syntax of the XSLT template is given here and this helps in optimizing the stylesheets.

<xsl: template match="*|/">
<xsl: apply-templates/>
</xsl: template>

How does XSLT template work?

XSLT is used to resolve the problems by applying template specification so the script of an XSLT has defined rules called templates which in turn has a block of codes to apply a specific logic. To select a particular element in a source file we could use a match attribute along with the template. And the content is placed between the <xsl: template>. Templates are of two types: A Match attribute and a named attribute. With the node selection, the XPATH expression takes select attribute even by taking multiple nodes like root/child or child1/child2 and so on which results in selecting two element nodes.

As templates as types similarly we have a way to invoke templates that instructs the XSLT processor to apply the rule.

1. <xsl: apply-templates>: Using a matching pattern the processor searches for a node. Or in another way, we can say using a concept called mode.

2. <xsl: call-template>: With the specific name, this template is been invoked. Before we use this a unique name should be assigned. This is also named as calling named templates.

In the following sections, we find the classes and the name which is been invoked with the pattern matchings. Let’s take an example

<xsl: apply-templates select="//java.io. Class[@xmi.sno]">

// So this instruction finds the class matching the attribute sno.
In contrast, we need to invoke the template with the name so the instruction is stated as follows which is found frequently in all the stylesheet.

<xsl:call-template name="educators"/>

To tackle unbalanced output like this we could use <xsl: text> inside the template.

<xsl: template name="start-frame"> <tr></xsl:template>

The above statement doesn’t work and XSLT engine couldn’t accept non-functional code fragments. This is overcome by using.

<xsl: template name="new">
<xsl:text disable-output-escaping="yes"><! [CDATA[<tr>]]></xsl:text>
</xsl: template>

To find the exact information in an XML document match attribute is been used. We will use employee.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="dept.xsl"?>
<cdept>
<faculty>friedman</faculty>
<faculty>Jackrose</faculty>
<faculty>Hardy</faculty>
<principal>Thomas heielxher</principal>
</cdept>

Every time if we need a faculty element to be displayed in the output, the following XSLT code will find the element using the template.

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="faculty">
Found a advisor!
</xsl:template>
</xsl:stylesheet>

So here we didn’t call of the root element which a well-formed XML file could expect but instead, we have filtered out by using template format here to display only faculty element.

Examples

XML sheet source code is given below.

Example #1 : Template function using count()

bill.xml

<?xml version="1.0" encoding="utf-8"?>
<bill.ref.no.book>
<bill.ref.no>
<invoice>RAM Requirement</invoice>
<number>X-900-677</number>
<year>2008</year>
</bill.ref.no>
<bill.ref.no>
<prefix>Database Doc</prefix>
<number>T-988-900</number>
<year>2010</year>
</bill.ref.no>
</bill.ref.no.book>
bill. xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bill.ref.no.book">
<xsl:variable name="bil">
<section class="demo2">
<xsl:text disable-output-escaping="yes">RAM Requirement</xsl:text>
</section>
</xsl:variable>
<xsl:variable name="exam">
<xsl:value-of select="./bill.ref.no/invoice"/>
</xsl:variable>
<xsl:variable name="loop">
<xsl:value-of select="./bill.ref.no/number"/>
<xsl:if test="following::bill.ref.no/number">;</xsl:if>
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="./bill.ref.no/year"/>
</xsl:variable>
<div class="new">
<xsl:value-of select="concat($bil,' – ',$exam,' Nos. ',$loop,'-',$year)"/>
</div>
</xsl:template>
</xsl:stylesheet>

Explanation

In the above code snippet, the template match is given to the root element, and using if condition the exact match is found and the result is displayed here.

Output:

XSLT Template 1

Example #2

XML

<?xml version="1.0" encoding="UTF-8"?>
<Topics>
<article id="a3423">
<name>K-Nearest Neighbours</name>
<month>January</month>
<author>Tim Mark </author>
<feedback > five rating</feedback>
</article>
<article id="y5643">
<name>AWS Service</name>
<month>April</month>
<author>David lousi </author>
<feedback> medium rating</feedback>
</article>
<article id="l6754">
<name>Pythonfor-Loop</name>
<month>March</month>
<author> Danny Jack </author>
<feedback> High rating</feedback>
</article>
</Topics>

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>Content- EDUCBA</h2>
<xsl:apply-templates select = "Topics/article" />
</body>
</html>
</xsl:template>
<xsl:template match = "Topics/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 = 16px;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "name">
First Name:<span style = "color:brown;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "month">
Last Name:<span style = "color:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "author">
Nick Name:<span style = "color:blue;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "feedback">
Marks:<span style = "color:gray;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>

Explanation

In the above code snippet, we had mentioned the template match along with that we addressed the <xsl: apply-templates> rule element for the child nodes too to create an output from the several data.

Output:

XSLT Template 2

Example #3

XML file

<TRdatabase>
<CustomerQuery>
<CustomerNumber>554</CustomerNumber>
<CName>Rana Chtarabosh</CName>
<SchDate>2017-04-06T18:51:01.60+04:30</SchDate>
<AmountFair>900</AmountFair>
<TeamName>Monica raawt</TeamName>
<JobNumber>2545</JobNumber>
<SectorNO>1224</SectorNO>
<Obligations>remarks</Obligations>
<TravelCost>1500</TravelCost>
<Mode>international</Mode>
</CustomerQuery>
<CustomerQuery>
<CustomerNumber>550</CustomerNumber>
<CName>Rana Chtarabosh</CName>
<SchDate>2019-05-04T18:52:01.60+04:30</SchDate>
<AmountFair>1100</AmountFair>
<TeamName>Atul raawt</TeamName>
<JobNumber>1234</JobNumber>
<SectorNO>1234</SectorNO>
<Obligations>remarks</Obligations>
<TravelCost>1500</TravelCost>
<Mode>international</Mode>
</CustomerQuery>
</TRdatabase>
.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="TRdatabase/CustomerQuery"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="SchDate">
<td>
<xsl:value-of select="format-dateTime(.,'[M01]/[D01]/[Y0001]')" />
</td>
</xsl:template>
<xsl:template match="CustomerQuery">
<tr>
<xsl:apply-templates select="SchDate"/>
</tr>
</xsl:template>
</xsl:stylesheet>

Explanation

The above example uses three template matches to print the date and time and the template rule is primarily applied to the child node and the output is shown as:

Output:

XSLT Template 3

Conclusion

As we have seen how the template rules describe the processing of a particular tag in the XML content file. The code inside them is processed by the processor and the output is displayed in the browser. I hope that this introduction would help with XSLT basic template rule learning.

Recommended Articles

This is a guide to XSLT Template. Here we discuss Definition, syntax, How does XSLT template work? examples with code implementation. You may also have a look at the following articles to learn more –

  1. XML file format
  2. XML DOM
  3. XML ampersand
  4. XML HttpRequest

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW