Chandu
Chandu

Reputation: 2049

How to select node which has a parent with some attributes

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>

Upvotes: 5

Views: 10465

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

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.

Upvotes: 3

Jayy
Jayy

Reputation: 2436

This will give you all nodes having atleast one attribute

//*[count(./@*) &gt; 0]

Upvotes: 0

penartur
penartur

Reputation: 9912

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

Upvotes: 4

Related Questions