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 Loop
 

XSLT Loop

Updated April 10, 2023

XSLT Loop

 

 

Definition of XSLT Loop

XSLT loop is an iteration construct where input items are sequenced and performs the iteration process. Programmers need to use a basic concept of the for-each template to call the variables number of times as there is no direct loop syntax. XSLT has an inbuilt recursive process to loop the respective element.

Watch our Demo Courses and Videos

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

Syntax:

The Syntax id depicted as follows

<xsl:call-template name="iterate">
<xsl:with-param name="ctr" select="$ctr + $increment"/>
</xsl:call-template>

How loop works in XSLT?

Looping construct is perfect in other programming languages like using the keyword ‘for’. Even in XSLT it is quite similar but with the keyword <xsl:for-each> as it has no loops or any mutual variables. Though they are not a traditional loop still programmers use them for content node change iteration. The job that is done with the templates is equally done with the looping. Usually, XSLT prefers Recursion for the looping technique as it calls itself until some condition is met. The other method is to use <call-template> which has a default value of XSL:param which is explained in the above syntax.

In XSLT 2.0 the iteration would be looks like:

<xsl:template name="doprocess">
<xsl:param name="count" select="1" />
<xsl:if test="$count">
// code
<xsl:call-template name="doprocess">
<xsl:with-param name="count" select="$count - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>

To loop the code around 10 times we need to go repeat them ‘n’ no. of times. To provoke a loop, we use a recursive template as follows like giving template name and param name by select attribute and parallelly incrementing the index value.

<xsl:template match="/">
<xsl:call-template name="newsample" />
</xsl:template>
<xsl:template name="newsample">
<xsl:param name="ptr" select="0" />
<xsl:param name="total" select="9" />
<xsl:if test="not($ptr" = $total)">
<xsl:call-template name="newsample">
<xsl:with-param name="ptr"" select="$ptr" + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>

To process the request for looping an URL 5 times and the response is:

<xsl:include href="educba.blog.xxx.xml"/>
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:variable name="openconn" select="'http://10.11.1.05/OpenLink'"/>;
<dp:url-open target="{$openconn}" response="responsecode" timeout="20" ></dp:url-open>
<dp:set-variable name="'var://context/details/ReasonCode'" value="'2'"/>
<xsl:message dp:priority="info">
message flow
</xsl:message>
<!--<xsl:with-param name="count" select="2" />
<xsl:param name="count" select="2"/>-->
<xsl:call-template name="Loopmethod">
<xsl:with-param name="count" select="5"/>
</xsl:call-template>

Examples

Example #1

XML file

<?xml version="1.0" encoding="UTF-8"?>
<body>
<h1>A new Demo is Released</h1>
<p>This theme demonstrates an Looping Activities:</p>
<ul>
<li> Python Course</li>
<li> Data Warehousing</li>
</ul>
</body>

The XSLT code is given as

<?xml version="1.0" encoding="US-ASCII" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" omit-xml-declaration="no" indent="yes" />
<xsl:template match="/">
<xsl:call-template name="loop">
<xsl:with-param name="k">4</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="k"></xsl:param>
<xsl:choose>
<xsl:when test="$k&gt; 1">
<xsl:text>Inside the body</xsl:text>
<xsl:text> </xsl:text> <!-- newline -->
<xsl:call-template name="loop">
<xsl:with-param name="k">
<xsl:number value="number($k)-1" />
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:text>Outside the body</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template></xsl:stylesheet>

Explanation

When we execute the above XSL file against an XML document the loop iterates till count 3 and exit the loop with the result Outside the body.

Output:

xml 1

Example #2

XML

<?xml version="1.0" encoding="utf-8"?>
<Cricketr>
<india recurs="3"/>
<Bangladesh recurs="2"/>
<newZealand recurs="5"/>
</Cricketr>

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="/Cricketr/*">
<content>
<xsl:call-template name="demo">
<xsl:with-param name="true">
<xsl:value-of select="@recurs"/>
</xsl:with-param>
</xsl:call-template>
</content>
</xsl:template>
<xsl:template name="demo">
<xsl:param name="true">1</xsl:param>
<xsl:param name="stop">1</xsl:param>
<xsl:param name="step">1</xsl:param>
<xsl:value-of select="name()"/>
<xsl:text/>
<xsl:if test="$true &lt; $stop">
<xsl:call-template name="demo">
<xsl:with-param name="stop">
<xsl:value-of select="$stop"/>
</xsl:with-param>
<xsl:with-param name="true">
<xsl:value-of select="$true + $step"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Explanation

The loop is getting executed for each value of the cricketer and ends with the stop limit ‘1’.’

Output:

XSLT Loop 2

Example #3

XML

<?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>
<head>
<title>YouTuber Listing</title>
</head>
<body>
<table>
<tr>
<th>name</th>
<th>E-mail Address</th>
</tr>
<xsl:for-each select="YoutubeList/Subscriber">
<tr>
<td><xsl:value-of select="sname"/></td>
<td><xsl:value-of select="email"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XSL

<?xml version="1.0" encoding="utf-8"?>
<YoutubeList>
<Subscriber>
<sname>Subhatra</sname>
<email>[email protected]</email>
</Subscriber>
<Subscriber>
<sname>Blppiki</sname>
<email>[email protected]</email>
</Subscriber>
</YoutubeList>

Explanation

The above XSL file is iterated by the URL and the HTML code is Output as below:

Output:

XSLT Loop 3

Example #4

XML

<?xml version="1.0" encoding="UTF-8"?>
<Collection>
<Type>
<Product>Henry</Product>
</Type>
<Type>
<Product>Henry</Product>
</Type>
<Type>
<Product>Colin</Product>
</Type>
<Type>
<Product>Henry</Product>
</Type>
<Type>
<Product>Monty</Product>
</Type>
<Type>
<Product>Colin</Product>
</Type>
</Collection>

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="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:key name="Type-name" match="Type" use="Product" />
<xsl:template match="Collection">
<xsl:variable name="copy">
<xsl:for-each select="Type">
<xsl:value-of select="count(key('Type-name', Product))" />
</xsl:for-each>
</xsl:variable>
<xsl:if test="string-length(translate($copy, '2', ''))!=0">
There are loop.
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Explanation

Here we built a string of occurrences of ‘two’. The loop gets iterated and finds the duplicates. And the result is shown as below:

Output:

XSLT Loop 4

Example #5

XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Exams>
<Database>
<Subject>CSE</Subject>
<colgcode>102</colgcode>
<QPCode>
<dept name = "Complier Design" SNo = "1255">
<yyyy CNo = "2" kkk = "CD"/>
<yyyy CNo = "3" kkk = "MI"/>
<yyyy CNo = "4" kkk = "AI"/>
<yyyy CNo = "6" kkk = "RT"/>
<yyyy CNo = "12" kkk = "DAA"/>
</dept>
</QPCode>
</Database>
<Database>
<Subject>CSE</Subject>
<colgcode>102</colgcode>
<QPCode>
<dept name = "Complier Design" SNo = "1255">
<yyyy CNo = "2" kkk = "CD"/>
<yyyy CNo = "3" kkk = "MI"/>
<yyyy CNo = "4" kkk = "AI"/>
<yyyy CNo = "6" kkk = "RT"/>
<yyyy CNo = "12" kkk = "DAA"/>
</dept>
</QPCode>
</Database>
<Database>
<Subject>CSE</Subject>
<colgcode>102</colgcode>
<QPCode>
<dept name = "Complier Design" SNo = "1255">
<yyyy CNo = "2" kkk = "CD"/>
<yyyy CNo = "3" kkk = "MI"/>
<yyyy CNo = "4" kkk = "AI"/>
<yyyy CNo = "6" kkk = "RT"/>
<yyyy CNo = "12" kkk = "DAA"/>
</dept>
</QPCode>
</Database>
</Exams>

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="end" select="number(6)" />
<xsl:variable name="increment" select="number(1)"/>
<xsl:template match="/">
<table>
<xsl:for-each select="//Database">
<tr>
<xsl:variable name="start" select="number(4)"/>
<xsl:call-template name="loop">
<xsl:with-param name="ctr" select="$start"/>
</xsl:call-template>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="loop">
<xsl:param name = "ctr"/>
<xsl:if test="$ctr &lt;= $end">
<td><xsl:value-of select="QPCode/dept/yyyy[@CNo=$ctr]/@kkk"/></td>
<xsl:call-template name="loop">
<xsl:with-param name="ctr" select="$ctr + $increment"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Explanation

The above code uses a loop to fill a table with a row and a column. here the subject along with QPcode is selected and the respective field is iterated based on the given number. Here in this example, the number given is ‘4’ which takes into the loop.

Output:

XSLT Loop 5

Advantages

1. The Added advantage is looping is for sorting.
2. The templates we use call by themselves and does the calculation with the parameters until the respective node is encountered.

Conclusion

To the end Though XSLT desperately has no direct loop some alternative functionality is defined. All the work reported examples defined above are executed well in the runtime environment. Some aspects of XSLT1.0 is ignored and other equivalent forms are perfectly executed in XSLT2.0.

Recommended Articles

This is a guide to XSLT Loop. Here we discuss definition, syntax, and How loop work in XSLT? with examples. You may also have a look at the following articles to learn more –

  1. XML file format
  2. XML DOM
  3. XML Mapping
  4. XML File

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