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

Questions about XML that are not covered by the other forums should go here.
mariomueller
Posts: 28

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

Wed Aug 08, 2018 12:36 pm

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
Radu
Posts: 5689

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

Wed Aug 08, 2018 3:01 pm

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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests