Project

Profile

Help

Default XPath namespace with JAXP?

Added by Anonymous over 15 years ago

Legacy ID: #6828466 Legacy Poster: Robert Henley (robert-henley)

Can I use the JAXP API, JDOM, and set a default namespace for XPath processing? Can I get at a Saxon-specific API from JAXP that would let me set a default XPath namespace? I modified the ApplyXPathJAXP.java sample application to return a namespace URI for the default namespace (""), but it is not called when XPath elements have no explicit namespace. I am calling this program with the JDOM memory model option. For example, the following XML is correctly processed with the query //f:item (returning "This is a test"), but gives no result with the query //item. <test xmlns="http://localhost/f"> <item>This is a test</item> </test> The XPath namespace context is declared as follows: xpath.setNamespaceContext( new NamespaceContext() { public String getNamespaceURI(String s) { System.err.println("getNamespaceURI('"+s+"')"); if (s.equals("")) { return "http://localhost/f"; } else if (s.equals("f")) { return "http://localhost/f"; } else { return null; } } public String getPrefix(String s) { return null; } public Iterator getPrefixes(String s) { return null; } } ); Any help would be appreciated!


Replies (1)

RE: Default XPath namespace with JAXP? - Added by Anonymous over 15 years ago

Legacy ID: #6829264 Legacy Poster: Michael Kay (mhkay)

Saxon's implementation of the JAXP XPath interface is the class net.sf.saxon.xpath.XPathEvaluator. This has a method getStaticContext() which returns a JAXPXPathStaticContext object, which has a method setDefaultElementNamespace(String uri). So it's: ((XPathEvaluator)xpath).getStaticContext().setDefaultElementNamespace("http://default.uri.com/"); Michael Kay http://www.saxonica.com/

    (1-1/1)

    Please register to reply