I'm trying to use XPath to find all elements that have an element in a given namespace.
For example, in the following document I want to find the foo:bar and doodah elements
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:foo="http://foo.example.com">
<foo:bar quux="value">Content</foo:bar>
<widget>Content</widget>
<doodah foo:quux="value">Content</doodah>
</root>
I know I can use the following XPath expression to load all attributes from a given namespace
"//@*[namespace-uri()='http://foo.example.com']"
However:
- This doesn't give me the elements, just the attributes and
- where elements contain multiple attributes from that namespace, this XPath will return a result per-attribute rather than per-element
Is it possible to get what I want, or do I just have to gather the attributes and calculate the unique set of elements they correspond to ?
EDIT: I was given the following answer by Dimitre Novatchev. I didn't realise that you could nest predicates inside predicates like this:
"//*[@*[namespace-uri()='http://foo.example.com']]"
Specifically this says "Any element that has any attribute that has namespace-uri = '...'"