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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:18:28+00:00 2026-05-27T09:18:28+00:00

Ok. I have an attribute in an xml document that I know will occur

  • 0

Ok. I have an attribute in an xml document that I know will occur more than once. Using C# I loop through all the nodes that have this attribute. I know how to count the occurrence of an element using xpath…

count("//x/y@b")

and so on.
But is there a way that I can get the n-th value of a node that I am on… for example

<?xml version="1.0"?>
<x>
   <y/>
   <y/>
   <y/>
</x>

Let’s say I was looping through that programatically using c#. And lets say I was on the second element. Is there any way using xpath that I could figure out that I am on the 2nd node? I guess I am just trying to find my position in the iteration. Any ideas? Currently scouring the internet. If I find it out I will be sure to let you know.

Thanks.

UPDATE: CAN’T SEEM to get my stuff to work
Ok. I thought I would update my question. I can’t seem to get any of your suggestions working…

<Template>
 <TemplateData>
  <ACOData>
   <POPULATION_PATIENT_ID>6161</POPULATION_PATIENT_ID>
   <PATIENT_ID>4329</PATIENT_ID>
  </ACOData>
  <ACOData>
   <POPULATION_PATIENT_ID>5561</POPULATION_PATIENT_ID>
   <PATIENT_ID>4327</PATIENT_ID>
  </ACOData>
  <ACOData>
   <POPULATION_PATIENT_ID>6160</POPULATION_PATIENT_ID>
   <PATIENT_ID>4321</PATIENT_ID>
  </ACOData>
  <ACOData>
   <POPULATION_PATIENT_ID>5561</POPULATION_PATIENT_ID>
   <PATIENT_ID>4320</PATIENT_ID>
  </ACOData>

That is the XML that I am using. But I can’t seem to get the correct count. I am always coming up with zero?

encounter = Int32.Parse((patElm.CreateNavigator().Evaluate("count(/Template/TemplateData/ACOData/POPULATION_PATIENT_ID[.='" + populationPatID + "']/preceding-sibling::ACOData/POPULATION_PATIENT_ID[.='"+populationPatID+"'])")).ToString());

The above is the code that I am attempting to use to get the correct value… Note my count function

count(/Template/TemplateData/ACOData/POPULATION_PATIENT_ID[.='" + populationPatID + "']/preceding-sibling::ACOData/POPULATION_PATIENT_ID[.='"+populationPatID+"'])"
  • 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-27T09:18:29+00:00Added an answer on May 27, 2026 at 9:18 am

    To get the second such element in the document use:

    (//x/y[@b])[2]
    

    Suppose you want to go the other way. That is, you have one of these nodes and you want to know its overall position. In general, for any expression <expr> the following is true:

    $n = count((<expr>)[$n]/preceding::*[count(.|<expr>)=count(<expr>)])
    

    That is, the position of the Nth element selected by <expr> can be found by counting all the preceding elements also selected by that expression. Using similar techniques, we can find the position of some node that would be selected by a more general expression, within the set of all nodes selected by that expression.

    For example, suppose we have the following document:

    <x>
        <y b="true"/>
        <y b="true"/>
        <y/>
        <y/>
        <x><y b="true"/><y/><y b="true">77</y></x>
        <y/>
        <y/>
    </x>
    

    And we want to know the position in the document of the node at /*/*/y[.='77'] among all nodes selected by //x/y[@b]. Then use the following expression:

    count(/*/*/y[.='77']/preceding::*[count(.|//x/y[@b])=count(//x/y[@b])]) + 1
    

    A more specific one-off solution looks like this:

    count(/*/*/y[.='77']/preceding::y[parent::x and @b]) + 1
    

    Result (in both cases):

    4
    

    Note: It’s assumed that /*/*/y[.='77'] and (<expr>)[$n] above actually select some node in the document. If not, the result will be an erroneous 1 due to adding 1 to the result of the count. For this reason, this method is probably most useful when working on a context node or when it is guaranteed that your initial expression selects a node. (Of course, initial error checking can be employed, as well.)

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

Sidebar

Related Questions

I know I could have an attribute but that's more work than I want
I have XmlDocument. I don't know XPath, I only know that this xml document
I have an XML document that contains a series of item nodes that look
I have a heavily nested XML document that I need to load into my
I have a WIX XML document that contains 2,000+ file tags. I am trying
Basically I have an xml document and the only thing I know about the
I have an XSLT stylesheet that processes an XML document to produce HTML. I've
I have some xml that looks like this: <rootElement attribute=' > '/> This is
I need an application that goes through an xml file, changes some attribute values
I have an xsd document that starts with: <?xml version=1.0 encoding=UTF-8?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema> <xs:import

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.