net.grelf
Class NamespaceContext

java.lang.Object
  extended by net.grelf.NamespaceContext
All Implemented Interfaces:
javax.xml.namespace.NamespaceContext

public class NamespaceContext
extends java.lang.Object
implements javax.xml.namespace.NamespaceContext

For use with javax.xml.xpath.XPath.
NB: Unprefixed namespaces cause great complications with XPath - typically nodes are not found when you expect them to be. So when processing XML that uses an unprefixed namespace, pretend there is one and set it in this context. Eg, AstroGrid's VOTABLE XML has xmlns="http://www.ivoa.net/xml/VOTable/v1.1" so all of its elements are unprefixed. That's fine but when using XPath to analyse the structure it is necessary to invent a prefix. The following code snippet finds all the TR elements in a VOTABLE, which is a common requirement.

                String xpathToTR = "/vot:VOTABLE/vot:RESOURCE/vot:TABLE/vot:DATA/vot:TABLEDATA/vot:TR";

                try
                {
                        net.grelf.grip.NamespaceContext nsContext =
         new net.grelf.NamespaceContext ("vot", "http://www.ivoa.net/xml/VOTable/v1.1");
                        XPATH.setNamespaceContext (nsContext);   // XPATH is a static constant, to avoid repeated construction
                        javax.xml.xpath.XPathExpression compiledXPath = XPATH.compile (xpathToTR);
                        org.w3c.dom.NodeList trElements =
         (org.w3c.dom.NodeList) compiledXPath.evaluate (voTable, javax.xml.xpath.XPathConstants.NODESET);
 

(org.w3c.dom is supplied in Java SE but it's right down at the end of the package list in the API documentation.)


Constructor Summary
NamespaceContext(java.lang.String prefix, java.lang.String namespaceURI)
          Construct a context containing one prefix-URI pair.
 
Method Summary
 void addNamespace(java.lang.String prefix, java.lang.String namespaceURI)
          Add a further prefix-URI pair to the context.
 java.lang.String getNamespaceURI(java.lang.String prefix)
           
 java.lang.String getPrefix(java.lang.String namespaceURI)
          Returns null if namespaceURI is not found.
 java.util.Iterator getPrefixes(java.lang.String namespaceURI)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NamespaceContext

public NamespaceContext(java.lang.String prefix,
                        java.lang.String namespaceURI)
Construct a context containing one prefix-URI pair.

Method Detail

addNamespace

public void addNamespace(java.lang.String prefix,
                         java.lang.String namespaceURI)
Add a further prefix-URI pair to the context.


getNamespaceURI

public java.lang.String getNamespaceURI(java.lang.String prefix)
Specified by:
getNamespaceURI in interface javax.xml.namespace.NamespaceContext

getPrefix

public java.lang.String getPrefix(java.lang.String namespaceURI)
Returns null if namespaceURI is not found.

Specified by:
getPrefix in interface javax.xml.namespace.NamespaceContext

getPrefixes

public java.util.Iterator getPrefixes(java.lang.String namespaceURI)
Specified by:
getPrefixes in interface javax.xml.namespace.NamespaceContext