See this page:
http://www.ibm.com/developerworks/library/x-javaxpathapi.html
It explains how a Namespace can be registered on the XPath factory to
be able to retrieve
elements.
I have written the following class:
public class HtmlNamespace implements NamespaceContext
{
public String getNamespaceURI(String prefix)
{
if (prefix == null) {
throw new NullPointerException("Null prefix");
}
else if ("html".equals(prefix)) {
return "
http://www.w3.org/1999/xhtml";
}
else if ("xml".equals(prefix)) {
return XMLConstants.XML_NS_URI;
}
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String uri)
{
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String uri)
{
throw new UnsupportedOperationException();
}
}
which can be used as follows:
XPath xp = XPathFactory.newInstance().newXPath();
xp.setNamespaceContext(new HtmlNamespace());
xp.evaluate("/html:HTML/html:BODY[1]/html:DIV[1]", dom,
XPathConstants.NODE);
Hope watij is going to include this in their XPath element
identification because at the moment if
a site has the <html xmlns="
http://www.w3.org/1999/xhtml"> attribute,
watij fails to retrieve any elements
through XPath.
AM