Reputation: 7189
In the below XML snippet what are the namespaces of a, c, d and e? Reference to documentation or specifications would be appreciated.
<a xmlns="http://domain/a">
<pre:b xmlns:pre="http://domain/b">
<c/>
<d xmlns="">
<e/>
</d>
</pre:b>
</a>
Also, what Java frameworks respect the official namespace defaulting? I have tride org.w2c.* DOM packages, however it does not seem to resolve the namespace URI correctly? For example, something with similar functionality to.
String namespace = DocumentParser.parse().
getElement("a").
getElement("b").
getElement("c").
getNamespaceURI();
Upvotes: 6
Views: 3999
Reputation: 108879
To the best of my knowledge, all the standard XML APIs in Java support namespaces. Many of the APIs were written before namespaces were created (or became popular - I can no longer remember). You often need to enable support:
public class NS {
private static void print(Node node) {
Queue<Node> nodes = new LinkedList<Node>();
nodes.add(node);
while (!nodes.isEmpty()) {
node = nodes.poll();
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
nodes.add(list.item(i));
}
System.out.format("%s %s %s%n", node.getPrefix(), node.getLocalName(),
node.getNamespaceURI());
}
}
public static void main(String[] args) throws Exception {
String xml = "<a xmlns=\"http://domain/a\">"
+ "<pre:b xmlns:pre=\"http://domain/b\">" + "<c/>" + "<d xmlns=\"\">"
+ "<e/>" + "</d>" + "</pre:b>" + "</a>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(
new InputSource(new StringReader(xml)));
print(doc.getDocumentElement());
}
}
This code will print:
null a http://domain/a
pre b http://domain/b
null c http://domain/a
null d null
null e null
Upvotes: 5
Reputation: 63126
From what I can tell it would be as follows.
I am basing this off of this specification. Below is a summary quote to help as well.
If there is a default namespace declaration in scope, the expanded name corresponding to an unprefixed element name has the URI of the default namespace as its namespace name. If there is no default namespace declaration in scope, the namespace name has no value. The namespace name for an unprefixed attribute name always has no value. In all cases, the local name is local part (which is of course the same as the unprefixed name itself).
Upvotes: 13
Reputation: 1500514
A namespace declared using xmlns="..."
becomes the default for any elements within it (until the default is then redeclared in a deeper element, of course). So you end up with:
The relevant spec section is the XML names spec, section 6.2:
The scope of a default namespace declaration extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner default namespace declarations. In the case of an empty tag, the scope is the tag itself.
A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
If there is a default namespace declaration in scope, the expanded name corresponding to an unprefixed element name has the URI of the default namespace as its namespace name. If there is no default namespace declaration in scope, the namespace name has no value. The namespace name for an unprefixed attribute name always has no value. In all cases, the local name is local part (which is of course the same as the unprefixed name itself).
Upvotes: 8