(xalan-j) : XPathAPI and namespaces 2006-02-14 - By sol myr
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. <div><FONT face=Verdana>I'm having a problem with XPATH and namespaces.<BR >Could anyone please help ?<BR><BR>I'm using apache XPathAPI, with java 1.4.<BR >It works fine as long as there are no namespaces.<BR>Unfortunately, when adding namespaces to the xml, XPathAPI fails to find the desired element (returning null).</FONT></div> <div><FONT face=Verdana></FONT> </div> <div><FONT face=Verdana>--- code ----------------------------------------</FONT></div> <div><FONT size=2>String xml=<BR> "< ns1:body xmlns :ns1='tempuri' >" +</FONT></div> <div><FONT size=2> "< ns1:status > OK < / ns1:status >" +<BR> "< / ns1:body >";<BR></FONT></div> <div> <FONT size=2>// Parse the above xml string:<BR>DocumentBuilder docBuilder= DocumentBuilderFactory.newInstanc().newDocumentBuilder();<BR>Document doc =docBuilder.parse(new InputSource(new StringReader(xml)));<BR>N ode root=doc.getDocumentElement();<BR> <BR>// Tell XPathAPI that "ns1" is the prefix for namespace "tempuri" :<BR>Element nsResolverNode=doc .createElement("NamespaceResolver");<BR>nsResolverNode.setAttribute("xmlns:ns1", "tempuri");<BR> <BR>// This returns "null", instead of the desired < status > element... why ?<BR>Node subNode = XPathAPI.selectSingleNode(root, "ns1 :body/ns1:status", nsResolverNode);<BR> </FONT></div> <div><FONT face =Verdana>--- end code -------------------------------------</FONT></div> <div> <BR><BR>What am i doing wrong ? Do you know of a better way to do it ?<BR></div> <div>Thanks very much.</div><p> <hr size=1>Brings words and photos together (easily) with<br> <a href="http://us.rd.yahoo.com/mail_us/taglines/PMall/*http://photomail.mail .yahoo.com">PhotoMail </a> - it's free and works with Yahoo! Mail.
|
|