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+"'])"
To get the second such element in the document use:
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: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:
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:A more specific one-off solution looks like this:
Result (in both cases):
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 erroneous1due 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.)