Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > java xerces xpath fails with namespace

Reply
Thread Tools

java xerces xpath fails with namespace

 
 
Martin Honnen
Guest
Posts: n/a
 
      02-10-2006


jacksu wrote:

> <?xml version='1.0' encoding='UTF-8'?><soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
> xmlnssd="http://www.w3.org/2001/XMLSchema"
> xmlnssi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><mynode
> xmlns="http://mynamespace">mytext</mynode></soap:Body></soap:Envelope>
>
> I tried:
> //soap:Envelope/soap:Body/mynode/text()
>
> If I gives prefix to mynode, then everything works fine.


You need to use a prefix in the XPath expression, no need to change the
input XML but for the XPath you need a prefix bound to
http://mynamespace to select those elements.
See
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
Simply make sure you use a prefix e.g.
pf1:mynode
and your NamespaceContext returns the URI http://mynamespace for that
prefix.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
jacksu
Guest
Posts: n/a
 
      02-10-2006
Excellent!! That works!!

Thanks a lot.

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      02-23-2006
I believe that I have prefixed my xpath properly, but I get an
XPathExpressionException when I evaluate it.

The xpath looks like this (note the "xhtml" prefix):


/xhtml:html/xhtml:body//xhtml:div[@class='reviewlist']


The source XML document is a garden-variety XHTML web page whose root
html element declares the document's default namespace as being
http://www.w3.org/1999/xhtml.


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>

<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">


As I understand it, writing the html element like this declares that it
belongs to the http://www.w3.org/1999/xhtml namespace. So, to evaluate
an xpath for this document, my javax.xml.xpath.XPath must have a
namespace context set. Here is my implementation of the the
javax.xml.namespace.NamespaceContext interface (note this
implementation accommodates mutlitple namespaces by use of a
java.util.HashMap - a tip from
http://www.onjava.com/pub/a/onjava/2.../12/xpath.html - lest a part
of the web page be in another language and its element has, say,
xml:lang="fr").


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.NamespaceContext;

public class NamespaceContextImpl implements NamespaceContext {

private Map map;

/**
* A contructor that instantiates a new java.util.HashMap in which
* namespace URIs will be mapped to prefixes.
*
* This method is inherited from the implemented NamespaceContext
interface.
*/
public NamespaceContextImpl() {
map = new HashMap();
}

/**
* Adds a prefix and namespace URI pair to this
* NamespaceContextImpl's HashMap.
*
* This method is not inherited from the implemented
* NamespaceContext interface.
*/
public void setNamespaceURI(String prefix, String namespaceURI) {
map.put(prefix, namespaceURI);
}

/**
* Gets the namespace URI mapped to the given
* prefix from this NamespaceContextImpl's HashMap.
*
* This method is inherited from the implemented NamespaceContext
interface.
*/
public String getNamespaceURI(String prefix) {
return (String)map.get(prefix);
}

/**
* Gets the prefix to which the given namespace
* URI is mapped in this NamespaceContextImpl's
* HashMap.
*
* This method is inherited from the implemented
* NamespaceContext interface.
*/
public String getPrefix(String namespaceURI) {

Set keys = map.keySet();

// Loop through the prefixes until one is found
// whose corresponding namespace URI matches
// the namespace URI passed to this method.
// Return that prefix.
for(Iterator i = keys.iterator(); i.hasNext(); ) {
String prefix = (String)i.next();
String uri = (String)map.get(prefix);
if(uri.equals(namespaceURI)) return prefix;
}

// If prefix is found with a namespace URI matching the
// namespace URI passed to this method, return null.
return null;
}

/**
* This method is inherited from the implemented
* NamespaceContext interface.
*/
public Iterator getPrefixes(String namespaceURI) {
return null;
}

}


I then use this NamespaceContext like this:


javax.xml.xpath.XPathFactory factory = XPathFactory.newInstance();
javax.xml.xpath.XPath xpath = factory.newXPath();

NamespaceContextImpl nsctx = new NamespaceContextImpl();
nsctx.setNamespaceURI("xml", "http://www.w3.org/XML/1998/namespace");
nsctx.setNamespaceURI("xhtml", "http://www.w3.org/1999/xhtml");
xpath.setNamespaceContext(nsctx);


For the evaluate method, I need a org.xml.sax.InputSource, which i get
like this:


java.net.URL url = new
java.net.URL("http://www.mywebpage.com/index.html");
java.net.HttpURLConnection huc =
(java.net.HttpURLConnection)url.openConnection();
org.xml.sax.InputSource ins = new
org.xml.sax.InputSource(huc.getInputStream());


Now I can evaluate the xpath and, I hope, get a org.w3c.dom.NodeList:


org.w3c.dom.NodeList nl = (
org.w3c.dom.NodeList)xpath.evaluate(
"/xhtml:html/xhtml:body//xhtml:div[@class='reviewlist']"
ins,
javax.xml.xpath.XPathConstants.NODESET
);



The result I get from calling this method, though, is a
javax.xml.xpath.XPathExpressionException, I think. At least, when I
catch the exception and call its toString() method, that what it says
it is. When I call its getCause() method, though, I get:

java.net.ConnectExcpetion: Connection timed out.


Its stack trace looks like this:


com.sun.org.apache.xpath.internal.jaxp.XPathImpl.e valuate(XPathImpl.java:475)
my.package.WebsiteHandler.startElement(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.startE lement(Unknown
Source)

org.apache.xerces.parsers.AbstractXMLDocumentParse r.emptyElement(Unknown
Source)

org.apache.xerces.impl.XMLNSDocumentScannerImpl.sc anStartElement(Unknown
Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl$FragmentContentDispatcher.dispatch(Unknown
Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanDocument(Unknown
Source)
org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.parse( Unknown Source)

org.cochrane.sitebuilder.servlet.WebsiteBuilder.pa rseWebSiteLayoutXML(Unknown
Source)
org.cochrane.sitebuilder.servlet.WebsiteBuilder.se rvice(Unknown
Source)
javax.servlet.http.HttpServlet.service(HttpServlet .java:810)
org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)

org.jboss.web.tomcat.filters.ReplyHeaderFilter.doF ilter(ReplyHeaderFilter.java:81)

org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:202)
org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:17
org.jboss.web.tomcat.security.CustomPrincipalValve .invoke(CustomPrincipalValve.java:39)
org.jboss.web.tomcat.security.SecurityAssociationV alve.invoke(SecurityAssociationValve.java:153)

org.jboss.web.tomcat.security.JaccContextValve.inv oke(JaccContextValve.java:59)

org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)

org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)

org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:14

org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:856)
org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:744)
org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
org.apache.tomcat.util.net.MasterSlaveWorkerThread .run(MasterSlaveWorkerThread.java:112)
java.lang.Thread.run(Thread.java:595)




Is it an XPathExpressionException? Is it a java.net.ConnectException?
What's going on?

 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      02-23-2006
Greg wrote:
> java.net.ConnectExcpetion: Connection timed out.


That's not a namespace problem, or shouldn't be. Namespaces are just
strings in URI format; the system never attempts to retrieve anything
from that URI. (At least, not unless you're getting involved in the
Semantic Web world, which is a different set of issues.) Hence,
namespaces don't have connections and don't time out.

It looks like the problem is during your attempt to retrieve your source
document, since it's reporting that the problem is in the parser.


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      02-23-2006
> It looks like the problem is during your attempt to retrieve your source
> document, since it's reporting that the problem is in the parser.
>

So there's something wrong with this source:

http://www.cochrane.org/reviews/en/topics/60_new.html

?

I certainly have no trouble retrieving that page in a browser (without
any noticeable connection time delay). And I'm told by
http://validator.w3.org that it's "valid" XHTML, so I'm under the
impression that its a well-formed XML document.

 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      02-24-2006
Greg wrote:
> So there's something wrong with this source:
> http://www.cochrane.org/reviews/en/topics/60_new.html
> ?


There's something wrong with your connection to the server, given that
you're getting a timeout message. Which may be a bug in your networking
code, or may be that you're running too slow and letting the connection
lapse, or may be something else. Without pounding on your machine, I
can't tell which.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      02-24-2006
Thanks for your response and suggestions, Joe.

The sum of the networking code, I belive, is just that which produces
a org.xml.sax.InputSource for the javax.xml.xpath.XPath's
evaluate(String expression, InputSource source, QName returnType)
method:

java.net.URL url = new
java.net.URL("http://www.exampleurl.com/index.html");
java.net.HttpURLConnection huc =
(java.net.HttpURLConnection)url.openConnection();
org.xml.sax.InputSource ins = new
org.xml.sax.InputSource(huc.getInputStream());

I'm not a master of networking code. The above code is something which
I more or less copy-and-pasted and which has "just worked" (with
several different URLs) for weeks up until a few days ago.

Just fyi, an update:

Every now and again the thrown XPathExpressionException's getCause()
method returns:

org.xml.sax.SAXParseException: Premature end of file

instead of

java.net.ConnectExcpetion: Connection timed out.

It's interesting (and frustrating) given that I can detect no change in
my code or in the XML source document between when this was working
last week and a few days ago when this XPathExpressionException started
showing up.

Here's the stack trace of the org.xml.sax.SAXParseException returned
from the getCause() method of the XPathExpressionException:

org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
org.apache.xerces.jaxp.DocumentBuilderImpl.parse(U nknown Source)
com.sun.org.apache.xpath.internal.jaxp.XPathImpl.e valuate(XPathImpl.java:46
org.cochrane.sitebuilder.contenthandler.WebsiteHan dler.startElement(Unknown
Source)
org.apache.xerces.parsers.AbstractSAXParser.startE lement(Unknown
Source)
org.apache.xerces.parsers.AbstractXMLDocumentParse r.emptyElement(Unknown
Source)
org.apache.xerces.impl.XMLNSDocumentScannerImpl.sc anStartElement(Unknown
Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl$FragmentContentDispatcher.dispatch(Unknown
Source)
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanDocument(Unknown
Source)
org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.parse( Unknown Source)
org.cochrane.sitebuilder.servlet.WebsiteBuilder.pa rseWebSiteLayoutXML(Unknown
Source)
org.cochrane.sitebuilder.servlet.WebsiteBuilder.se rvice(Unknown Source)
javax.servlet.http.HttpServlet.service(HttpServlet .java:810)
org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doF ilter(ReplyHeaderFilter.java:81)
org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:202)
org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:17
org.jboss.web.tomcat.security.CustomPrincipalValve .invoke(CustomPrincipalValve.java:39)
org.jboss.web.tomcat.security.SecurityAssociationV alve.invoke(SecurityAssociationValve.java:153)
org.jboss.web.tomcat.security.JaccContextValve.inv oke(JaccContextValve.java:59)
org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:14
org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:856)
org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:744)
org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
org.apache.tomcat.util.net.MasterSlaveWorkerThread .run(MasterSlaveWorkerThread.java:112)
java.lang.Thread.run(Thread.java:595)

 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      02-24-2006
Greg wrote:
> I'm not a master of networking code. The above code is something which
> I more or less copy-and-pasted and which has "just worked" (with
> several different URLs) for weeks up until a few days ago.


May be a malfunction on the server's end rather than yours, then. Or in
the network. Timeouts and premature end of file could be either.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      02-27-2006
Well, the problem has gone away as mysteriously as it appeared.

For posterity, I can report only that the cause of the
XPathExpressionException was also occasionally "java.io.IOException:
stream is closed".

For now, given the variety of the causes, I'm pinning the ultimate
cause on my unkempt code, which I have now tidied up and documented
substantially.

Thanks for your help, Joe.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use Xerces for parsing XPath QQ XML 1 03-04-2008 12:21 PM
Does the xerces for c++ parser support XPath? honky XML 1 08-22-2005 03:47 PM
Upgrade of Xalan 1.2.2 and Xerces 1.4.4 to Xalan 2.6 and Xerces 2.6.2 cvissy XML 0 11-16-2004 07:06 AM
Run-Time Check Failure #2 using XPath with Xalan and Xerces Francesc Guim Bernat XML 0 04-30-2004 01:13 AM
Xpath apache xerces/xalan dom3 Volker Jordan XML 0 01-22-2004 12:08 AM



Advertisments