0

I am using xpath via npm and I am searching for an XPath 1.0 expression, such that the search after 'name' and 'run' should result in the following output.

Finding the first "parent node" using "//*[contains(name(), 'name')]" already works, but I don't know if it's possible to find a child by a part of its name (any number of levels below)?

input:

<root>
    <NameX>
       <getLenght>
         <unit>m</unit>
       </getLenght>
       <getStatus>
         <status>0</status>
       </getStatus>
   </NameX>
   <Name2>
       <getStatus>
         <status>0</status>
       </getStatus>
       <running>
         <bit>true</bit>
       </running>
   </Name2>
</root>

output:

<running>
    <bit>true</bit>
</running>
6
  • Did you try //*[contains(name(), 'name')]//*[contains(name(), 'run')]?
    – Andersson
    Aug 25, 2017 at 13:01
  • I am not sure if i tried his already - i'll have a look on monday, thanks.
    – Max
    Aug 26, 2017 at 18:47
  • Can you clarify what exactly you want your XPath to do?
    – Andersson
    Aug 28, 2017 at 6:48
  • @Andersson I want to search in a tree and and with the result I want to create a tree structure in HTML, where the found values are shown as opened/highlighted. Therefore I want to be able to search for a parent node including 'name' and subnodes that include 'run' or 'getStatus', thus the output should be: '<getStatus> <status>0</status> </getStatus> <getStatus> <status>0</status> </getStatus> <running> <bit>true</bit> </running>'
    – Max
    Aug 28, 2017 at 7:05
  • Still confused... Do you want to get parent node based on its child node? SOmething like //*[contains(name(), 'name') and .//*[contains(name(), 'run')]]?
    – Andersson
    Aug 28, 2017 at 7:23

1 Answer 1

0

Andersson's answer is correct:

//*[contains(name(), 'name')]//*[contains(name(), 'run')]

Advanced example:

If you want to search for a parent node 'Name' which has at least a child element e.g. 'getLen' or getStat', then you can use this query:

//*[contains(name(), 'Name')]//*[contains(name(), 'getLen') or 
contains(name(), 'getStat')]`

output:

<NameX>
    <getLenght>
        <unit>m</unit>
    </getLenght>
    <getStatus>
        <status>0</status>
    </getStatus>
</NameX>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.