Interface NamespaceAware
- All Known Subinterfaces:
Parent
- All Known Implementing Classes:
Attribute
,CDATA
,Comment
,Content
,DocType
,Document
,Element
,EntityRef
,LocatedCDATA
,LocatedComment
,LocatedDocType
,LocatedElement
,LocatedEntityRef
,LocatedProcessingInstruction
,LocatedText
,ProcessingInstruction
,Text
Namespace
context. All the core JDOM classes are NamespaceAware
(Parent
and subtypes, Content
and subtypes, and
Attribute
). You can use the methods that this interface provides
to query the Namespace context.
JDOM2 introduces a consistency in reporting Namespace context. XML standards do not dictate any conditions on Namespace reporting or ordering, but consistency is valuable for user-friendliness. As a result JDOM2 imposes a useful order on the Namespace context for XML content.
The order for Namespace reporting is:
- If the item 'has' a Namespace (Element, Attribute) then that Namespace is reported.
- The remaining Namespaces are reported in alphabetical order by prefix.
The XML namespace (bound to the prefix "xml" - see
Namespace.XML_NAMESPACE
) is always in every scope. It is always
introduced in Document
, and in all other NamespaceAware instances it
is introduced if that content is detached.
See the individualised documentation for each implementing type for additional specific details. The following section is a description of how Namespaces are managed in the Element class.
The Element Namespace Scope
The 'default' Namespace is a source of confusion, but it is simply the Namespace which is in-scope for an Element that has no Namespace prefix (prefix is "" but it could have any Namespace URI). There will always be exactly one Namespace that is in-scope for an element that has no prefix.
All Elements are in a Namespace. Elements will be in
Namespace.NO_NAMESPACE
unless a different Namespace was supplied as
part of the Element Constructor, or later modified by the
Element.setNamespace(Namespace)
method.
In addition to the Element's Namespace, there could be other Namespaces that are 'in scope' for the Element. The set of Namespaces that are in scope for an Element is the union of five sets:
XML |
There is always exactly one member of this set,
Namespace.XML_NAMESPACE .
This set cannot be changed. |
---|---|
Element |
There is always exactly one member of this set, and it can be retrieved
or set with the methods Element.getNamespace() and
Element.setNamespace(Namespace) respectively.
|
Attribute |
This is the set of distinct Namespaces that are used on Attributes. You
can modify the set by adding and removing Attributes to the Element.
NOTE:
The |
Additional |
This set is maintained by the two methods Element.addNamespaceDeclaration(Namespace)
and Element.removeNamespaceDeclaration(Namespace) . You can get the full set
of additional Namespaces with Element.getAdditionalNamespaces()
|
Inherited | This last set is somewhat dynamic because only those Namespaces on the parent Element which are not re-defined by this Element will be inherited. A Namespace is redefined by setting a new Namespace with the same prefix, but a different URI. If you set a Namespace on the Element (or add a Namespace declaration or set an Attribute) with the same prefix as another Namespace that would have been otherwise inherited, then that other Namespace will no longer be inherited. |
Since you cannot change the Namespace.XML_NAMESPACE, and the 'inherited' Namespace set is dynamic, the remaining Namespace sets are the most interesting from a JDOM perspective. JDOM validates all modifications that affect the Namespaces in scope for an Element. An IllegalAddException will be thrown if you attempt to add a new Namespace to the in-scope set if a different Namespace with the same prefix is already part of one of these three sets (Element, Attribute, or Additional).
- Since:
- JDOM2
- Author:
- Rolf Lear
-
Method Summary
Modifier and TypeMethodDescriptionObtain a list of all namespaces that are in scope for this content, but were not introduced by this content.Obtain a list of all namespaces that are in scope for the current content.Obtain a list of all namespaces that are introduced to the XML tree by this node.
-
Method Details
-
getNamespacesInScope
Obtain a list of all namespaces that are in scope for the current content.The contents of this list will always be the combination of getNamespacesIntroduced() and getNamespacesInherited().
See
NamespaceAware
documentation for details on what the order of the Namespaces will be in the returned list.- Returns:
- a read-only list of Namespaces.
-
getNamespacesIntroduced
Obtain a list of all namespaces that are introduced to the XML tree by this node. Only Elements and Attributes can introduce namespaces, so all other Content types will return an empty list.The contents of this list will always be a subset (but in the same order) of getNamespacesInScope(), and will never intersect getNamspacesInherited()
- Returns:
- a read-only list of Namespaces.
-
getNamespacesInherited
Obtain a list of all namespaces that are in scope for this content, but were not introduced by this content.The contents of this list will always be a subset (but in the same order) of getNamespacesInScope(), and will never intersect getNamspacesIntroduced()
- Returns:
- a read-only list of Namespaces.
-