Binding a single namespace

11 July 2008

The XPath expression that finds the titles of all of Laiq’s books will be //pre:book[pre:author=”Laiq”]/pre:title/text(). However, you have to map the prefix pre to the URI.


It’s a little strange that the NamespaceContext interface doesn’t have a default implementation in the Java software development kit (JDK) or JAXP, but it doesn’t. However, it’s not hard to implement. Following simple implementation is for one namespace. You should map the xml prefix as well.

import java.util.Iterator;
import javax.xml.*;
import javax.xml.namespace.NamespaceContext;
 
public class PersonalNamespaceContext implements NamespaceContext {
 
    public String getNamespaceURI(String prefix) {
        if (prefix == null) throw new NullPointerException("Null prefix");
        else if ("pre".equals(prefix)) return "http://www.example.org/books";
        else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;
        return XMLConstants.NULL_NS_URI;
    }
 
    // This method isn't necessary for XPath processing.
    public String getPrefix(String uri) {
        throw new UnsupportedOperationException();
    }
 
    // This method isn't necessary for XPath processing either.
    public Iterator getPrefixes(String uri) {
        throw new UnsupportedOperationException();
    }
 
}

del.icio.us:Binding a single namespace  digg:Binding a single namespace  spurl:Binding a single namespace  wists:Binding a single namespace  simpy:Binding a single namespace  newsvine:Binding a single namespace  blinklist:Binding a single namespace  furl:Binding a single namespace  reddit:Binding a single namespace  fark:Binding a single namespace  blogmarks:Binding a single namespace  Y!:Binding a single namespace  smarking:Binding a single namespace  magnolia:Binding a single namespace  segnalo:Binding a single namespace  gifttagging:Binding a single namespace

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Binding a single namespace

Leave a Reply