Hi! I'm using dom4j's great XPath processing to do extensive unit 
testing of XHTML. Changing the pages to be explicitly XHTML broke all my 
XPaths, and I'm wondering how to fix that. Ideally, I'd like the default 
namespace in my documents to be the default namespace in my XPaths.


For starters, let me explain the situation. Consider this simple document:

    <html><head><title>Hi!</title></head><body><p>Hello</p></body></html>

You could test for the presence of the paragraph like this:

        assertThat(document.selectNodes("//p").size(),is(1));

However, suppose I add the proper doctype:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

And then start the document like this:

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

Suddenly, my test fails! The XPath no longer matches.


This has been discussed in the archives before. The solution mentioned 
is to set it up so that the XPaths have an explicit namespace, and then 
to set a namespace map in the DocumentFactory. But for me, that would 
require changing hundreds of test assertions, and in a way that would 
make them harder to read.

Is there some way I can fix this without changing my tests? I tried 
explicitly telling the DocumentFactory that "" should map to the XHTML 
namespace, and in the debugger it looks like the Document object has 
that right. But I still can't do a selectNodes call and have it return 
anything.

Any help is appreciated.

Thanks,

William




-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to