Issue 7: Error with namespace prefix names while executing XPath selectors
Status:  New
Owner: ----
Reported by benoit.sautel@gmail.com, Sep 24, 2012
What steps will reproduce the problem?

Consider the following GWTUnit test.

{{{
    public void testNamespaces()
    {
        XmlParser parser = new XmlParser();
        String xml =
                "<prefix:root xmlns:prefix=\"urn:prefix:test\"><prefix:name>foo</prefix:name></prefix:root>";
        Document rightPrefix =
                parser.parse(xml, "xmlns:prefix=\"urn:prefix:test\"");
        // Works since namespace declaration is the same as in the document
        assertEquals("foo",
                rightPrefix.getRoot().selectValue("/prefix:root/prefix:name"));

        Document wrongPrefix =
                parser.parse(xml, "xmlns:otherName=\"urn:prefix:test\"");
        // Exception thrown (null message)
        assertEquals(
                "foo",
                wrongPrefix.getRoot().selectValue(
                        "/otherName:root/otherName:name"));
    }
}}}

We would expect the two tests to success because namespace identification is URI instead of prefix name.
The first test works, but the second one throws an exception with a null message:
`name.pehl.totoe.xml.client.XPathException: (null): null
	at name.pehl.totoe.xml.client.internal.NodeImpl.selectNode(NodeImpl.java:236)
	at name.pehl.totoe.xml.client.internal.NodeImpl.selectValue(NodeImpl.java:299)
	at name.pehl.totoe.xml.client.internal.NodeImpl.selectValue(NodeImpl.java:290)
(...)
`

I'm using totoe 0.4. May I misuse totoe's API?

Oct 12, 2012
#1 ak...@tanoshi.net
I am also running into this problem but only if I am using the lib from GWTTestCase.

In my real app it works correctly... would be nice to be able to unit test though.
Oct 13, 2012
#2 benoit.sautel@gmail.com
For me, it neither works in GWTUnit test nor in "real app".

Do you mean your compiled code works? And what about dev mode?

I wrote the following Javascript code that relies on Sarissa.js (and jQuery) and it works (at least on Firefox and Chrome):

var executeXPathExpression = function (document, prefixName) {
    Sarissa.setXpathNamespaces(document, 'xmlns:' + prefixName + '="urn:prefix:test"');
    var result = document.documentElement.selectSingleNode('/' + prefixName + ':root/' + prefixName + ':name');
    console.log(result);
    console.log($(result).text());
};
var parseXml = function() {
    var stream = '<prefix:root xmlns:prefix="urn:prefix:test"><prefix:name>foo</prefix:name></prefix:root>';
    var document = (new DOMParser()).parseFromString(stream, "text/xml");
    executeXPathExpression(document, 'prefix');
    executeXPathExpression(document, 'foo');
};
parseXml();

I was not sure that Sarissa supports this use case, but it seems to be the case.
Oct 13, 2012
#3 ak...@tanoshi.net
For me it does work in production as well as dev mode but throws the exact exception if I use it in GWTTestCase.

This actually concerns me so I am going to investigate. I'll post back here if I find anything.
Oct 13, 2012
#4 ak...@tanoshi.net
I have been playing around for a while now and I can't seem to make it break in dev mode or compiled mode. 

I thought my test was bad so I decided to stick you're code into my project and still works in dev mode.

Now in unit test it does break as you said and my test also breaks.

I'm sure the issue is coming from the same place... I just couldn't make it happen in dev mode or compiled. Going try to track down whats causing it.

Project I created for testing:
https://github.com/anozaki/TotoeTestApp

Activity with test:
https://github.com/anozaki/TotoeTestApp/blob/master/src/main/java/net/tanoshi/test/totoe/client/home/HomeActivity.java

Unit test:
https://github.com/anozaki/TotoeTestApp/blob/master/src/test/java/net/tanoshi/test/totoe/client/home/TotoeGwtTest.java
Oct 14, 2012
#5 ak...@tanoshi.net
Spent a lot more time then I wanted and don't really have a clear answer. But I think it has something to do with a race condition in parsing and when selectNode is called.

looks like Sarissa uses XMLHttpRequest or loadXML to load up the xml but I believe in both cases the variable 'readyState' will become 4 (complete) when parsing is done.

In my case... Every time I get this exception, this value is undefined. 


Oct 15, 2012
#6 benoit.sautel@gmail.com
After investigation, I reached to get it work in my real app.

I integrated my unit test code in my real app and it works on Chrome and Firefox. What browsers did you test with?

I don't know why it did not work in my real app when I tested it. It may not be for the same reason. I wrote a unit test to be sure there was a bug and the unit test confirmed it.

I see only one reason that could explain why this code works in the real app but not in a GWTUnit test. For me, the only thing that differs is the runtime environment. Maybe GWTUnit embedded browser does not support this XML functionality? 
What do you think about that?