tags:

views:

80

answers:

2

Given the following xml, how do I write an xPath query to pull nodes where attribute the foo exists?:

<node1>
  <node2>
    <node3 foo='bar'></node3>
    <node3></node3>
    <node3 bar='foo'></node>
    <node3 foo='foobar'></node3>
  </node2>
</node1>
+2  A: 

Short and sweet:

//*[@foo]

Of course you should use a more specific expression. But with [@attributeName] you get all nodes which have that attribute.

Felix Kling
+1  A: 

If you use and xpath, this maybe can help you:

count(//*[@foo])

it will return count of node/child that have attribute foo

fritz