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 171933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:04:32+00:00 2026-05-11T13:04:32+00:00

I need to count the number of elements in an XML file that have

  • 0

I need to count the number of elements in an XML file that have a particular value (to verify uniqueness). The XML file looks like this:

EDIT: I updated the original ‘simplified’ XML with the actual hairy mess someone designed. Unfortunately, this is going to make all the previous Answers really confusing and wrong unless edited.

<root>   <ac>    <Properties>      <Property Name='Alive'>       <Properties>         <Property Name='ID'>          <Properties>            <Property Name='Value'>             <long>11007</long>            </Property>          </Properties>         </Property>       </Properties>      </Property>      <Property Name='Dead'>       <Properties>         <Property Name='ID'>          <Properties>            <Property Name='Value'>             <long>11008</long>            </Property>          </Properties>         </Property>       </Properties>      </Property>      ...      <Property Name='MostlyDeadAllDay'>       <Properties>         <Property Name='ID'>          <Properties>            <Property Name='Value'>             <long>99001</long>            </Property>          </Properties>         </Property>       </Properties>      </Property>    </Properties>   </ac> </root> 

I am trying to define a variable to see how many of the properties at the Alive/Dead level have the long value (ID) as defined in a template parameter. Something along these lines (though I suspect this is wrong)…

<xsl:param name='parPropId'/> <xsl:variable name='countProperties'>    <xsl:value-of select='count(/root/ac/       Properties/Property/       Properties/Property[@Name = 'ID']/       Properites/Property[@Name = 'Value']/long = $parPropId)'/> </xsl:variable> 

There can be multiple Property elements defined at the ‘ID’ level. But I am fairly certain I can count on just one Property element (‘Value’) under ‘ID’, and only one ‘long’ element under ‘Value’.

[disclaimer] Whoever designed the overall XML file I’m stuck working with REALLY didn’t know how to structure XML (e.g., backwards use of attributes versus elements). I fear my XSLT thinking has temporarily been warped 🙂 as a result. [/disclaimer]

  • 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. 2026-05-11T13:04:33+00:00Added an answer on May 11, 2026 at 1:04 pm

    This XPath:

    count(//Property[long = '11007']) 

    returns the same value as:

    count(//Property/long[text() = '11007']) 

    …except that the first counts Property nodes that match the criterion and the second counts long child nodes that match the criterion.

    As per your comment and reading your question a couple of times, I believe that you want to find uniqueness based on a combination of criteria. Therefore, in actuality, I think you are actually checking multiple conditions. The following would work as well:

    count(//Property[@Name = 'Alive'][long = '11007']) 

    because it means the same thing as:

    count(//Property[@Name = 'Alive' and long = '11007']) 

    Of course, you would substitute the values for parameters in your template. The above code only illustrates the point.

    EDIT (after question edit)


    You were quite right about the XML being horrible. In fact, this is a downright CodingHorror candidate! I had to keep recounting to keep track of the ‘Property’ node I was on presently. I feel your pain!

    Here you go:

    count(/root/ac/Properties/Property[Properties/Property/Properties/Property/long = $parPropId]) 

    Note that I have removed all the other checks (for ID and Value). They appear not to be required since you are able to arrive at the relevant node using the hierarchy in the XML. Also, you already mentioned that the check for uniqueness is based only on the contents of the long element.

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

Sidebar

Related Questions

I've got some tests that need to count the number of warnings raised by
I have an array in jQuery, and I need to count the number of
I need to count all elements who has id like 'hi*'. For example, if
I need to render a specified number of elements from an XML source, where
I need to count the number of occurrences of elements of the second column
The following code is designed to count the number of elements that satisfy the
I need to count number of repeating and position where they repeat of all
In a table members(id, contractid, a,b,c,d) I need to count: the number of members
I need to count the total number of instances in which a 10-digit number
I need a regex to count the number of columns in a pipe delimited

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.