As an AJAX toolkit Google Web Toolkit is supports retrieving XML documents from the server, and supplies an XML parser to obtain information from those documents. However, using the DOM to navigate the XML tree can be annoying. Some sort of selection functionality would be useful.
Sarissa is successful at wrapping *most* oddities among the browsers, by requiring some extra configuration for IE (from the HOWTO):
Actually IE also needs the proprietaryTo demonstrate:
setPropertymethod for it's XPath implementation to work.
The namespaces used in the XPath query must be declared prior to executing the query. These functions are no-ops for other browsers, resulting in cross-browser support. The HOWTO continues:
Error: uncaught exception: [Exception... "An attempt was made to create or change an object in a way which is incorrect with regard to namespaces" code: "14" nsresult: "0x8053000e (NS_ERROR_DOM_NAMESPACE_ERR)" location: "sarissa_ieemu_xpath.js Line: 159"]Namespace used in query that are auto-detected by Mozilla:
Searching for "/exist:result/tei:test" correctly returns the single node "tei:test". However, in the following case, the very same query will result in an error on the console.
An XPath query on the above XML that uses namespace "tei" results in cryptic error NS_ERROR_DOM_NAMESPACE_ERR.
SolutionTurns out, Mozilla provides similar functionality to IE's "SelectionNamespaces" property, and this is wrapped in Sarissa.setXpathNamespaces() and works in a similar manner as IE. However, the Sarissa documentation seems to incorrectly indicate that it is unnecessary except when the "document features a default namespace", because "moz will rezolve non-default namespaces by itself". Using this function to explicitly define the namespaces fixes the error and returns the result of the selection.
I therefore modified the Eric Bessette's GWT wrapper library to take an string of namespaces to be used, and use the setXpathNamespaces() function (which seems to cover for IE's "SelectionNamespaces" property as well).
Alternative Methods of Selection
Aside from using XPath, there are other selection/querying methods I considered.
While researching selection solutions, I considered using GWTQuery. GWTQuery is a neat library which mimics jQuery syntax for selection. Incidentally, it uses XPath where available. Unfortanately it is made for HTML and doesn't seem to support XML too well.
I also considered converting the XML to JsonML. There are two forms of JsonML ( http://en.wikipedia.org/wiki/JsonML#Syntax, http://tech.groups.yahoo.com/group/json/message/1115 ): Array Form and the Object Form. Unfortunately, I found that selection in Array Form is still tedious, as the first two elements might be the tag name and a dictionary of the attributes, but, if there are no attributes, then the second element is already a node. This makes iterator through nodes just as tedious as using the DOM! At the very least, keep an empty dictionary for the attributes, so that I don't have to test every node to see if the second array element is a dictionary ( and thus an attribute ) or an element. Object Form is more explicit, keeping the attributes as a dictionary, and reserving the attributes "tagName" and "childNodes" to contain the name of the node, and an array of the child nodes respectively. In my opinion, a better solution would be that each node should be a dictionary, a tagName entry containing the name of the node, an attribute entry containing a dictionary of attributes, and a childNodes entry, containing an array of children. An example:
JsonML has the added advantage that all of the processing can be done on the server side (using the handy XSLT transform provided), and I might yet switch to using a modified version of it in the future. It is still good to have XPath support in GWT through Sarissa though. Though Sarissa seems not to be active, it uses feature sniffing, and should thus continue to work in any browser that remains backwards compatible. If GWTQuery gets better support for XML, I would probably use it in conjunction with both of them.