GPD
GPD

Reputation: 17

Getting a node and its specific sub node

I am trying to get a node and specific sub node. Let's say I have below XML.

<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
  <property>number one</property>
  <csvname>
     <name>one</name>
   </csvname>
  <first>Yash</first>
  <last>M</last>
  <age>22</age>
</person>
<person>
  <property>number two</property>
  <csvname>
     <name>two</name>
   </csvname>
  <first>Bill</first>
  <last>Gates</last>
  <age>46</age>
</person>
<person>
  <property>number three</property>
  <csvname>
     <name>three</name>
   </csvname>
  <first>Steve</first>
  <last>Jobs</last>
  <age>40</age>
</person>
</book>

I want to get root element along with matching person element(lets say person having name=one ), ie output like this :

<person>
  <property>number one</property>
</person>

How can I get above output using Java xpath.

I have tried several xpath expression but no luck.

Upvotes: 0

Views: 114

Answers (1)

supputuri
supputuri

Reputation: 14135

Here is the xpath.

 //name[.='one']/ancestor::person

Screenshot:

enter image description here

Upvotes: 1

Related Questions