1
votes

Say I have the following XML file:

<a xmlns:foo="http://foo"></a>   

I need to check whether the prefix foo is bound to http://foo or not. Whereby not bound could indicate that the said prefix does not exist at all or is bound to some other namespace URI. I already have a library that takes a Document object and an XPath expression and returns a (possibly empty) List of Nodes that exist at that XPath.

So what would be an expression that would check for the presence of a prefix foo in the top-most element (document element) bound to the namespace http://foo and that would yield one node for the above XML and zero nodes for the following XMLs:

<a xmlns:fooX="http://foo"></a>

and

< xmlns:foo="http://fooX"></a>

I tried, as a first step, to just get the value of that attribute using:

/*[@*[local-name()='foo']]

... but it seems that prefix-binding attributes are handled differently from "normal" attributes.

1

1 Answers

3
votes

If you want to do it with XPath then you have to use the namespace axis: /*[namespace::foo[. = 'http://foo']]. DOM Level 3 might provide different ways treating the namespace declarations as attributes and resolving prefixes, see http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespaceURI.