The document element is ns:body, so your XPath needs to be relative to that.  Either put a slash in front of your XPath to indicate you are starting at the document node (not the document element) or change your xpath to just ns1:status.

 


From: sol myr [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 14, 2006 6:23 AM
To: xalan-j-users@xml.apache.org
Subject: (xalan-j) : XPathAPI and namespaces

 

I'm having a problem with XPATH and namespaces.
Could anyone please help ?

I'm using apache XPathAPI, with java 1.4.
It works fine as long as there are no namespaces.
Unfortunately, when adding namespaces to the xml, XPathAPI fails to find the desired element (returning null).

 

--- code ----------------------------------------

String xml=
     "< ns1:body xmlns:ns1='tempuri' >" +

          "< ns1:status > OK < / ns1:status >" +
     "< / ns1:body >";

// Parse the above xml string:
DocumentBuilder docBuilder= DocumentBuilderFactory.newInstanc().newDocumentBuilder();
Document doc=docBuilder.parse(new InputSource(new StringReader(xml)));
N ode root=doc.getDocumentElement();

// Tell XPathAPI that "ns1" is the prefix for namespace "tempuri" :
Element nsResolverNode=doc.createElement("NamespaceResolver");
nsResolverNode.setAttribute("xmlns:ns1", "tempuri");

// This returns "null", instead of the desired < status > element... why ?
Node subNode = XPathAPI.selectSingleNode(root, "ns1:body/ns1:status", nsResolverNode);
 

--- end code -------------------------------------



What am i doing wrong ? Do you know of a better way to do it ?

Thanks very much.


Brings words and photos together (easily) with
PhotoMail - it's free and works with Yahoo! Mail.

Reply via email to