Skip to content
Advertisement

Find index of nested XML tag

suppose I have an XML like this:

JavaScript

What I want now is a way to get the index of the nested tag with a certain value. So for example the index of bar would be 1 (or 2 if you count from 1).

I have already done this using Apache CachedXPathApi:

JavaScript

Which works fine for getting the index from the attribute attr like this:

getIndex("/body/nested/@attr", "blub")

But I don’t know how to do this for the nested values. If I use /body/nested/name then it will obviously only ever count the name tags within nested which is not what I want.

How can I solve this, either by changing the Java Code or maybe even with a special XPath expression?

Advertisement

Answer

Just evaluate this XPath expression:

JavaScript

XSLT-based verification:

The following transformation simply evaluates the XPath expression and outputs the result of this evaluation:

JavaScript

When applied on the provided XML document:

JavaScript

the wanted, correct result is produced:

2


II. Update

In a comment the OP described another case, where there is no element in the XML document that satisfies the filtering condition. In such case the previous expression (above) returns 1 and this is incorrect.

Here is an XPath expression that returns the correct result in all cases:

JavaScript

This is the previous XPath expression, multiplied by another XPath expression (on its left) which evaluates to 0 if there is no element that satisfies the filtering predicate, and to 1 otherwise.

The XSLT-based verification shows that this XPath expression evaluates to the correct 1-based index values in the case when a satisfying element exists, and to 0 when no such element exists. Here we take advantage of the fact that the implicit conversion number(false()) is 0 and number(true()) is 1 .

Here is the latter example:

JavaScript

Here no element has a <name> child with string value 'bar'. And when we apply the transformation:

JavaScript

the correct, wanted result is produced:

0

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement