javax.xml.namespace

Interface NamespaceContext

public interface NamespaceContext

The NamespaceContext interface is a helper tool for XML parsing applications which need to know the mappings between XML namespace prefixes and namespace URI's. As such, it is closely related to the events {@link org.xml.sax.ContentHandler#startPrefixMapping(String,String)}, and {@link org.xml.sax.ContentHandler#endPrefixMapping(String)} in {@link org.xml.sax.ContentHandler}.

In what follows, it is important to note, that a single prefix can only be mapped to a single namespace URI at any time. However, the converse is not true: Multiple prefixes can be mapped to the same namespace URI's.

For example, in the case of an XML Schema parser, an instance of NamespaceContext might be used to resolve the namespace URI's of referenced data types, and element or attribute names, which are typically given as qualified names, including a prefix and a local name.

Since: JAXB 1.0

Author: JSR-31

Method Summary
StringgetNamespaceURI(String pPrefix)

Given a prefix, returns the namespace URI associated with the prefix.

StringgetPrefix(String pNamespaceURI)

This method returns a prefix, which is currently mapped to the given namespace URI.

IteratorgetPrefixes(String pNamespaceURI)

This method returns a collection of prefixes, which are currently mapped to the given namespace URI.

Method Detail

getNamespaceURI

public String getNamespaceURI(String pPrefix)

Given a prefix, returns the namespace URI associated with the prefix. More precisely, the following rules apply:

Prefix (Input)Namespace URI (Output)
{@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX} ("") The current default namespace URI or null, if there is no such default. (In which case the absence of a prefix indicates the absence of a namespace URI.)
{@link javax.xml.XMLConstants#XML_NS_PREFIX} ("xml") {@link javax.xml.XMLConstants#XML_NS_URI} ("http://www.w3.org/XML/1998/namespace")
{@link javax.xml.XMLConstants#XMLNS_ATTRIBUTE} ("xmlns") {@link javax.xml.XMLConstants#XMLNS_ATTRIBUTE_NS_URI} ("http://www.w3.org/2000/xmlns/")
Any other prefixThe namespace URI currently mapped to the prefix or null, if no such mapping is established.

Parameters: pPrefix The prefix being looked up in the list of mappings.

Returns: The Namespace URI to which the input prefix is currently mapped or null, if there is no such mapping.

Throws: IllegalArgumentException The input prefix is null.

getPrefix

public String getPrefix(String pNamespaceURI)

This method returns a prefix, which is currently mapped to the given namespace URI. Note, that multiple prefixes may be mapped to the namespace URI, in which case the returned prefix is undetermined. Do not make any assumptions on the order in such cases. It is a better choice to use {@link #getPrefixes(String)} instead, if you depend on some order

Namespace URI (Input)Prefix (Output)
Current default namespace URI {@link javax.xml.XMLConstants#DEFAULT_NS_PREFIX} ("")
{@link javax.xml.XMLConstants#XML_NS_URI} ("http://www.w3.org/XML/1998/namespace") {@link javax.xml.XMLConstants#XML_NS_PREFIX} ("xml")
{@link javax.xml.XMLConstants#XMLNS_ATTRIBUTE_NS_URI} ("http://www.w3.org/2000/xmlns/") {@link javax.xml.XMLConstants#XMLNS_ATTRIBUTE}

Parameters: pNamespaceURI The namespace URI being looked up in the list of mappings.

Returns: A prefix currently mapped to the given namespace URI or null, if there is no such mapping

Throws: IllegalArgumentException The input URI is null

getPrefixes

public Iterator getPrefixes(String pNamespaceURI)

This method returns a collection of prefixes, which are currently mapped to the given namespace URI. Note, that the collection may contain more than one prefix, in which case the order is undetermined. If you do not depend on a certain order and any prefix will do, you may choose to use {@link #getPrefix(String)} instead. The following table describes the returned values in more details:

Namespace URI (Input)Prefix collection (Output)
{@link javax.xml.XMLConstants#XML_NS_URI} ("http://www.w3.org/XML/1998/namespace") Collection with a single element: {@link javax.xml.XMLConstants#XML_NS_PREFIX} ("xml")
{@link javax.xml.XMLConstants#XMLNS_ATTRIBUTE_NS_URI} ("http://www.w3.org/2000/xmlns/") Collection with a single element: {@link javax.xml.XMLConstants#XMLNS_ATTRIBUTE}

Parameters: pNamespaceURI The namespace URI being looked up in the list of mappings or null, if there is no such mapping.

Returns: An unmodifiable {@link java.util.Iterator}: Using it's {@link java.util.Iterator#remove()} method throws an {@link UnsupportedOperationException}.

Throws: IllegalStateException The input URI is null