Reputation: 11
<xxx1 xmlns="hello">
<xxx2>
<xxx3>
<name>rule_1</name>
</xxx3>
</xxx2>
</xxx1>
I select node by "//*[namespace-uri()='hello']/*[local-name()='name']"
It should get //hello:xxx1/xxx2/xxx3/name
, and it does.
Now I try to get element . In reality, I don't know how much parent for <name>
will get <xxx1>
;
I try this code
node.getParent().getNamespaceURI() = "Hello"
and increase getParent()
amount to get <xxx1>
But the first time I call <xxx3>.getNamespaceURI()
it returns true
.
Is the namespace inherited?
How to get the element has or not has xmlns?
Sorry for my question was not clearly.
I'm trying to get the element which is the first declared namespace "hello".
<xxx1 xmlns="hello">
<xxx2>
<xxx3>
this three node which one is contained xmlns="hello"
, 'cause <xxx2>
and <xxx3>
was not declare xmlns
in the label.
Upvotes: 1
Views: 634
Reputation: 12817
Hello and Welcome to Stack Overflow!
Yes, namespaces are sort of inherited, but the terminology normally used is that, in your example, the <name>
element is in the scope of the namespace declaration xmlns="hello"
, so the <name>
element will be in the hello
namespace.
With DOM4J
, you can test whether an element is in a namespace or not like this:
boolean hasNamespace(Element e) {
return e.getNamespaceURI().length() > 0;
}
If the element is not in any namespace, getNamespaceURI()
returns an empty string.
I guess that you want to select the <name>
element, but you don't know at which level it be, i.e. how many parents it will have. You can always use this XPath expression:
Node node = doc.selectSingleNode("//*[namespace-uri() = 'foo' and local-name() = 'name']");
Upvotes: 0