|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.Iterator;
|
|
|
|
import javax.xml.XMLConstants;
|
|
import javax.xml.namespace.NamespaceContext;
|
|
import javax.xml.xpath.XPath;
|
|
|
|
import net.sf.saxon.xpath.XPathFactoryImpl;
|
|
|
|
public class XPathCompile {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
XPath xpath = new XPathFactoryImpl().newXPath();
|
|
xpath.setNamespaceContext(namespaceContext);
|
|
xpath.compile("//*:x");
|
|
}
|
|
|
|
private static final NamespaceContext namespaceContext = new NamespaceContext() {
|
|
@Override
|
|
public String getNamespaceURI(String prefix) {
|
|
if (prefix == null)
|
|
throw new IllegalArgumentException();
|
|
if (prefix.equals(XMLConstants.XML_NS_PREFIX))
|
|
return XMLConstants.XML_NS_URI;
|
|
if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE))
|
|
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
|
|
return XMLConstants.NULL_NS_URI;
|
|
}
|
|
|
|
@Override
|
|
public String getPrefix(String namespaceURI) {
|
|
if (namespaceURI == null)
|
|
throw new IllegalArgumentException();
|
|
if (namespaceURI.equals(XMLConstants.NULL_NS_URI))
|
|
return XMLConstants.DEFAULT_NS_PREFIX;
|
|
if (namespaceURI.equals(XMLConstants.XML_NS_URI))
|
|
return XMLConstants.XML_NS_PREFIX;
|
|
if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
|
|
return XMLConstants.XMLNS_ATTRIBUTE;
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public Iterator getPrefixes(String namespaceURI) {
|
|
String prefix = getPrefix(namespaceURI);
|
|
return prefix == null ? Collections.emptyIterator() : Arrays.asList(prefix).iterator();
|
|
}
|
|
};
|
|
|
|
}
|