Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select node which has a parent with some attributes

Tags:

xpath

How to select node which has a parent with some attributes. Eg: what is Xpath to select all expiration_time elements. In the following XML, I'm getting error if states elements has attributes, otherwise no probs.

Thanks

<lifecycle>
  <states elem="0">
    <expiration_time at="rib" zing="chack">08</expiration_time>
  </states>
  <states elem="1">
    <expiration_time  at="but">4:52</expiration_time>
  </states>
  <states elem="2">
    <expiration_time  at="ute">05:40:15</expiration_time>
  </states>
  <states elem="3">
    <expiration_time>00:00:00</expiration_time>
  </states>

</lifecycle>
like image 390
Chandu Avatar asked Feb 16 '12 08:02

Chandu


2 Answers

states/expiration_time[../@elem = "0"]?

like image 119
penartur Avatar answered Jan 01 '23 12:01

penartur


Use:

/*/*/expiration_time

This selects all expiration_time elements that are grand-children of the top-element of the XML document.

/*/*[@*]/expiration_time

This selects any expiration_time element whose parent has at least one attribute and is a child of the top element of the XML document.

/*/*[not(@*)]/expiration_time

This selects any expiration_time element whose parent has no attributes and is a child of the top element of the XML document.

/*/*[@elem = '2']/expiration_time

This selects any expiration_time element whose parent has an elem attribute with string value '2' and that is (the parent) a child of the top element of the XML document.

like image 37
Dimitre Novatchev Avatar answered Jan 01 '23 12:01

Dimitre Novatchev