Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3872948
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:58:52+00:00 2026-05-19T21:58:52+00:00

How can I count the number of leaf nodes coming off a particular XML

  • 0

How can I count the number of leaf nodes coming off a particular XML node using jQuery?
The XML in question looks similar to this. I want all leaf nodes coming off the <Errors> node.

<Errors>
    <ErrorParentCat>
        <ErrorTag/>
        <ErrorTag2/>
    </ErrorParentCat>
</Errors>

In this example I want <ErrorTag/> and <ErrorTag2/> counted only. The result should therefore be 2. Also <ErrorParentCat> is an example tag, there could be many within <Errors> with different names.

Once I have the number I would like a list of these leaf nodes too, if possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:58:52+00:00Added an answer on May 19, 2026 at 9:58 pm

    Assuming you already have an XMLDocument named xml:

    var $xml = $(xml),
        count = $xml.find('*').filter(function ()
        {
            return $(this).children().length === 0;
        }).length;
    console.log(count);
    

    You can also just pass the XML string directly to the jQuery function:

    var $xml = $('<Errors><ErrorParentCat><ErrorTag/><ErrorTag2/></ErrorParentCat></Errors>');
    // the rest is the same
    

    jsfiddle demo →


    Edit you said you wanted a list of those leaf nodes. In the code above, you’ve already got them:

    var $xml = /* whatever */,
        $leafNodes = $xml.find('*').filter(function ()
        {
            return $(this).children().length === 0;
        }),
        count = $leafNodes.length;
    

    Edit #2 as Tim Down has pointed out (see comments below), you cannot just pass the XML string to $() in IE (!@#$ing IE). You should use the jQuery 1.5 function $.parseXML() to parse an arbitrary well-formed XML string into an XMLDocument:

    var xmlDoc = $.parseXML('<Errors><ErrorParentCat><ErrorTag/><ErrorTag2/></ErrorParentCat></Errors>'),
        $xml = $(xmlDoc);
    /* the rest is unchanged */
    

    new jsfiddle demo →

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table and can count the number of rows using var count
How can I count the number of files in a directory using C on
How can I count the number of items in div that are hidden using
how can I count the number of white spaces in a file using PHP?
I have a question. How can I count number of posts of a tag
How can I count the number of rows in a csv file using powershell?
How can I count the number of null values using SSRS? The following expression
How can I count the number of times a particular string occurs in another
I would like to know how I can count the number of unique values
Is there a way I can programmatically count the number of links for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.