Reputation: 138240
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:
Is it possible to get what I want, or do I have to gather the attributes and calculate the unique set of elements they correspond to?
Upvotes: 22
Views: 41422
Reputation: 54111
You could try
//*[namespace-uri()='http://foo.example.com' or @*[namespace-uri()='http://foo.example.com']]
It will give you element foo:bar and element doodah (if you change tal:quux
to foo:quux
in your XML-data):
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:foo="http://foo.example.com" xmlns:tal="xxx">
<foo:bar quux="value">Content</foo:bar>
<widget>Content</widget>
<doodah foo:quux="value">Content</doodah>
</root>
Is that what you want?
Upvotes: 1
Reputation: 711
I'm not sure if this is what you mean, but by only deleting one char in your XPath you get all elements in a certain namespace:
//*[namespace-uri()='http://foo.example.com']
Upvotes: 4
Reputation: 243619
Use:
//*[namespace-uri()='yourNamespaceURI-here'
or
@*[namespace-uri()='yourNamespaceURI-here']
]
the predicate two conditions are or-ed with the XPath or
operator.
The XPath expression thus selects any element that either:
Upvotes: 28
Reputation: 1594
Your XPath expression is almost perfect. Instead of asking for attributes "@" ask for elements "" and it should work:
"//*[namespace-uri()='http://foo.example.com']"
Upvotes: 0