Page 1 of 1

get node depending of parent position (but nodename of parent is unknown)

Posted: Wed Aug 08, 2018 12:36 pm
by mariomueller
Hi all,

we have different XML files where we want to extract the value of a node with name <S_NAD> depending on the current position
We know that <S_NAD>-node is a grandchild of the root. Example

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<book>
<unknownName>
<S_NAD>some content</S_NAD>
</unknownName>
<unknownName>
<S_NAD>some content</S_NAD>
</unknownName>
</book>

The difficulty is, that the nodename of the child of the root varies. In the example it is: <unknownName>

I wrote a XPATH, but this works wrong:

Code: Select all

/*/*/S_NAD/parent::*[position()=1]
But this gives me:
1) XPath Location: /book[1]/unknownName[1]
2) XPath Location: /book[1]/unknownName[2]

where I only expected the first line.

Could anybody please help?
Thanks
Regards Mario

Re: get node depending of parent position (but nodename of parent is unknown)

Posted: Wed Aug 08, 2018 3:01 pm
by Radu
Hi Mario,

Some parentheses might help:

Code: Select all

(/*/*/S_NAD/parent::*)[position() = 1]
in order to first compute the entire sequence of parents and then to select from them the first one.

Or you can also try something like:

Code: Select all

/*/*/S_NAD[parent::*[count(preceding-sibling::*) = 1]]
Regards,
Radu