You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by sol myr <so...@yahoo.com> on 2006/02/14 15:23:13 UTC

(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)));
Node 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.