problem with xpath query (maybe namespace)

9 views
Skip to first unread message

tom

unread,
Jul 4, 2008, 3:53:50 AM7/4/08
to watij

Hi there,

I recently discovered a problem with xpath query. I think it is a xml
namespace problem. I hope somebody can help me on this one.

Here is my example code:

IE ie = new IE();
ie.start("http://www.gmx.de");
ie.waitUntilReady();

System.out.println("Divs: " + ie.divs().length());
System.out.println("//DIV: " + ie.htmlElements(SymbolFactory.xpath, "//
DIV").length());
System.out.println("//div: " + ie.htmlElements(SymbolFactory.xpath, "//
div").length());
System.out.println("//Div: " + ie.htmlElements(SymbolFactory.xpath, "//
Div").length());


And this is the console output:
Divs: 133
//DIV: 0
//div: 0
//Div: 0

As you can see the xpath expressions don't find the div elements. I
think it could be a namespace problem. Here is the beginning of the
ie.html() output:

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


Any one got the same problem?
Btw if someone found a way to use 'normal' xpath (not only uppercase
letters for nodenames) would be intresting too. (f.e.: '//a' works not
only '//A').

Best regards
Tom

johnny

unread,
Jul 4, 2008, 7:39:20 AM7/4/08
to watij
Hm that's a default namespace, so the div got that namespace
description.
I don't think that's a problem for watij, but maybe "//
namespace::xmlns/div" could work ...
Anyway the ie.divs() method found 133 divs, maybe
FinderFactory.xpath(String expression) would be an alternative ...

I also got that problem with the key-sensitive letters in the tags
("DIV", "div" and "Div").
In HTML these tags are the same, but in other languages (e.g. XML)
they could be different.
Therefore in my opinion i think it's not useful to modify the xpath
api, so that these tags are the same ...
I filtered them with the expression: "//DIV | //div | //Div"

tom

unread,
Jul 10, 2008, 8:12:43 AM7/10/08
to watij
Thanks. I'll try your suggestions.
I'm not too deep into xml. Maybe there is a solution to register that
namespaceresolver to the document before the query.

AM

unread,
Aug 7, 2008, 11:18:17 AM8/7/08
to watij
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
Reply all
Reply to author
Forward
0 new messages