I think you have an answer on stackoverflow. It would kind to flag it as the correct answer if it suits you. It is indeed a good one.

http://stackoverflow.com/questions/21801754/regarding-xpath-parsing-of-xml-with-default-namespace-using-saxon

On 15 Feb 2014, at 19:45, Pratap Nataraj <pratapmn@yahoo.com> wrote:


Hi,

I wanted some help/info related to xpath parsing of default namespace using saxon. I am using the Saxon-HE-9.5.1-3.jar to use xpath 2 features in my code. After including the saxon library in the class path, I am facing a issue parsing xpath for XML documents with default namespaces.

Sample XML in am using :
<?xml version="1.0" encoding="utf-8"?>
<RESPONSE  xmlns="http://www.abc.com/responseCode="200">
  <HEADER>
    <HITS>100</HITS>
  </HEADER>
</RESPONSE>

Valid XPATH: /RESPONSE/HEADER/HITS

Below are the cases where it works and does not work:
1. "XPATH Works" : When no namespace is specified
Example : <RESPONSE  responseCode="200">

2. "XPATH Works" : When namespace with prefix is given
Example : <RESPONSE  xmlns:res="http://www.abc.com/responseCode="200">

3. "XPATH does not work" : When default namespace with out prefix is given
Example : <RESPONSE  xmlns="http://www.abc.com/responseCode="200">


Can you please help me out on why saxon treats no namespace and default namespace in a different way ?
Also how do I solve the case of doing xpath for documents with default namespace.


Below is chunks of my code:
=====================
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import net.sf.saxon.xpath.XPathEvaluator;
import net.sf.saxon.xpath.XPathFactoryImpl;

.
.
.

DocumentBuilder builder;
Document doc;

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
builder = domFactory.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(b, 0, size));

XPathFactory factory = XPathFactoryImpl.newInstance(XPathConstants.DOM_OBJECT_MODEL);
XPathEvaluator xpathCompiler = (XPathEvaluator) factory.newXPath();
XPathExpression expr = xpathCompiler.compile(xpath);
NodeList childNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);


Thanks & Regards
Pratap



------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help