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 count
 

XSLT count

Updated April 18, 2023

XSLT count

 

 

Introduction to XSLT count

XSLT count function is defined to count elements with specific attribute names defined in the XML document. It neglects the arithmetic on a set of nodes to validate the number of counts. It adopts Transformation by matching the templates in a Style sheet against an XML document.

Watch our Demo Courses and Videos

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

Syntax

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:value-of select="count(root/child/line[name='attribute value'])" />
</xsl:template>
</xsl:stylesheet>

The syntax says the count function tells the XSLT processor to count each time in which an XML tag found at the XPATH expression. It returns a number value that specifies the number of sequence elements contained in a sequence of items that passes by. The empty set of the count is returned to ‘zero’.

How does the count function work in XSLT?

It is very common to prefer the count function in XSLT code to number all the elements that have a specific attribute role the native way to do this is. This function is always preferred with XSL commands like select with attributes so that the home browser displays the results of the count().

Number count (node path);

So, this is quite slow because whenever the XSLT processor sees an <root> element, it needs to traverse the tree to find all the previous <root> elements. Therefore, the most common way to use is which is designed in XPATH and XSLT. “//” returns all the nodes in the document that matches the given expression. Generally, the count() function takes a single argument with an XPath expression a path. The location path here counts all child elements of the current node.

<xsl:vname="condition" select="$numb[condition='expression']"/>
<xsl:vname="count" select="count($Condition)"/>

Using if statement to count() values. To find the numbers we need to do in the following manner.

<xsl:text> Dreams </xsl:text>
<xsl:if test=count( $night1/author) gt 2>
<xsl:text> xy </xsl:text>
<xsl:if>

Here an XPATH expression is included with an attribute test. Here the XPATH starts by making a function call that is a count()  which counts the number of lists in the sequence. Here is the sequence of author elements. Next XPath asks the XSLT engine to read the condition assigned greater than two(gt). The comparison is made with the sequence and the respective result is displayed.

To do word Count in a string

<xsl:sequence select="count(tokenize($stringvalue, '\W+')[. != ''])"/

Examples

The following examples show how to count up the given attribute values automatically to accomplish the task by matching a given XPath expression.

Example-1pass.xml

<passengers>
<passenger depDate="05/20/2019" military="yes">
<flast>venu</flast>
<fname>shankar</fname>
<Cost>5000</Cost>
</passenger>
<passenger depDate="06/06/2018" military="no">
<flast>joseph</flast>
<fname>sam</fname>
<Cost>5500</Cost>
</passenger>
<passenger depDate="08/12/2019" military="no">
<flast>Anoop</flast>
<fname>pradhan</fname>
<Cost>6000</Cost>
</passenger>
<passenger depDate="03/08/2019" military="yes">
<flast>jaoy</flast>
<fname>ancellita</fname>
<Cost>5000</Cost>
</passenger>
</passengers>

Here Is the stylesheet that illustrates the count()

pass. 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="passengers">

1. Number of passengers:

<xsl:value-of select="count(passenger)" />

2. Number of military person:

<xsl:value-of select="count(passenger[@military='yes'])" />

3. Number of passengers with no Military person’ attribute set:

<xsl:value-of select="count(passenger[not(@military)])" />

4. Number of comments in the ‘passengers’ element:

<xsl:value-of select="count(//comment())" />
</xsl:template>
<xsl:template match="passenger" />
</xsl:stylesheet>

Explanation

Here is the code with <xsl:value-of select which takes a count function and sets up a specific attribute we want to count. Here we get the count as 4 for the number of passengers. To achieve this, we simply passed an attribute to the count()  function. The below output shows the list of count values that we got so far.

Output:

XSLT count output 1

Example #2

person.xml

<?xml version="1.0"?>
<?xml-stylesheet type="development/xml" href="database.xslt"?>
<candidate>
<student born="2006" died="2008" id="01">
<sname>
<fname>Anand</fname>
<lname>Bobby</lname>
</sname>
<profession>Analytics</profession>
<profession>Big data engineer</profession>
<profession>Chef</profession>
</student>
<student born="2004" died="2007" id="02">
<sname>
<fname>D</fname>
<middle_name>E</middle_name>
<lname>F</lname>
</sname>
<profession>Go ibo</profession>
<passtime>Hubernate</passtime>
</student>
</candidate>

person.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="candidate">
<xsl:apply-templates select="student"/>
</xsl:template>
<xsl:template match="student">
Person <xsl:value-of select="position()"/>
of <xsl:value-of select="count(//student)"/>:
<xsl:value-of select="sname"/>
</xsl:template>
</xsl:stylesheet>

Explanation

This counts the number of candidates in the XML file.

Output:

XSLT count output 2

Example #3

Code Implementation to get the count from the list by adding with arithmetic operations.

pp.xml

<?xml version="1.0"?>
<Listnumbers>
<cat>4</cat>
<dog>3.2</dog>
<zebra>11</zebra>
</Listnumbers>

pp.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:template match="Listnumbers">
15 + count(*) =
<xsl:value-of select="15+count(*)" />
</xsl:template>
</xsl:stylesheet>

Explanation

Here in the above code, I chose the arithmetic plus to add with the count()  function.  So, the value of 15 is added with 3 as the number of child elements inside the list numbers is three. The great simplified Output looks like this.

Output:

XSLT count output 3

Example #4

cc.xml

<?xml version="1.0"?>
<countdemo>
<first f="1">
<first f="2">
<first>
<child>y21</child>
<child>y22</child>
</first>
</first>
</first>
<first f="1">
<first f="2">
<child>y11</child>
<child>y12</child>
</first>
</first>
<first f="1">
<child>y8</child>
<child>y9</child>
</first>
<first>
<child>y40</child>
<child>y41</child>
</first>
</countdemo>

cc.xsl

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
//first,         <xsl:value-of select="count(//first)"/>
//first[1],      <xsl:value-of select="count(//first[1])"/>
//first/child,       <xsl:value-of select="count(//first/child)"/>
//first/child[1],    <xsl:value-of select="count(//first/child[1])"/>
//first[1]/child[1], <xsl:value-of select="count(//first[1]/child[1])"/>
</xsl:template>
</xsl:stylesheet>

Explanation

An XML file when applied to XSLT gives the following output to return the number of nodes in the elements.

Output:

output 4

Example  #5 : count()  using specific values

st.xml

<?xml version="1.0" encoding="utf-8" ?>
<first>
<anchor>
<program>
<game>
<status>scheduled</status>
<announce>
<sdate>09/12/2010</sdate>
<time>09:00</time>
</announce>
</game>
<game>
<status>scheduled</status>
<announce>
<sdate>07/1/2010</sdate>
<time>08:00</time>
</announce>
</game>
<game>
<status>waiting</status>
<announce>
<sdate>05/12/2010</sdate>
<time>08:00</time>
</announce>
</game>
<game>
<status>waiting</status>
<announce>
<sdate>08/10/2010</sdate>
<time>08:00</time>
</announce>
</game>
</program>
</anchor>
<anchor>
<program>
<game>
<status>scheduled</status>
<announce>
<sdate>04/3/2010</sdate>
<time>08:00</time>
</announce>
</game>
<game>
<status>start</status>
<announce>
<sdate>06/6/2010</sdate>
<time>08:00</time>
</announce>
</game>
<game>
<status>start</status>
<announce>
<sdate>05/3/2010</sdate>
<time>08:00</time>
</announce>
</game>
<game>
<status>saleend</status>
<announce>
<sdate>02/12/2010</sdate>
<time>05:00</time>
</announce>
</game>
</program>
</anchor>
</first>

st.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="//anchor">
<xsl:variable name="count" select="count(current()//game/status[.='scheduled'])" />
<xsl:value-of select="$count" />
<xsl:choose>
<xsl:when test="$count = 2">
ONE output
</xsl:when>
<xsl:otherwise>
No Output
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Explanation

In the above XSLT code, we have counted how many programs with status=” scheduled” using value-of select. And if the count is ‘2’ we have declared a chosen keyword to display conditional statements. Below is the output we got.

Output:

output 5

Conclusion

Coming to an end, we have seen a simple way to count the XML elements in XSLT with a few examples. This is very helpful as it is designed to resist things simple for XSLT coders so we don’t want to pile on risky functions to process the number of values.

Recommended Articles

This is a guide to XSLT count. Here we discuss How does the count function works in XSLT along with the examples and outputs. You may also have a look at the following articles to learn more –

  1. XSLT substring
  2. XML ampersand
  3. PHP XML Reader
  4. XML Element

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