Xandor Leahte wrote:
> Hey there,
>
> I wish to introduce you to a problem that i get working on Javascript
> and XPath.
>
> Be r an XMLHttpRequest object; i want to make a request through a
> webpage inside my domain (so no security problem); with r i can handle
> r.responseText and r.responseXML: sometimes i can't use responseXML
> cause of no valid syntax of the document, so I've to use responseText.
> So, creating the DOM document like this way:
>
> var doc = new DOMParser().parseFromString(r.responseText, "text/
> xml")
I don't see why parseFromString on responseText would work when
responseXML could not be built.
> Then I can try to evaluate a XPath expression on doc, like:
>
> doc.evaluate(query, doc, null, 0, null)
>
> where query is a valid XPath expression. There's the problem: if I
> make a query like "//*[@id='foo']" or "//*" it works perfectly;
> otherwise if i make a query like "/html/body" or "/ol/li/a" or
> something without wildcard * included, the evaluate function returns
> null. I can't understand why if i dont use the wildcard query doesn't
> work (see: query works if I try to evaluate it in a "document" contest
> like in firebug/js console where my page is the "document" object).
Post a sample of the XML markup you parse with DOMParser. I suspect it
is a namespace problem i.e. you have
<html xmlns="http://www.w3.org/1999/xhtml">...</html>
in your responseText and then you parse that with DOMParser a XML DOM
document is built with the elements all belonging to the XHTML
namespace. In that case with doc.evaluate you need to pass in a
namespace resolver and use a prefix e.g.
doc.evaluate('xhtml:html/xhtml:body', doc, function(prefix) { if
(prefix === 'xhtml') return 'http://www.w3.org/1999/xhtml'; else return
null; }, 0, null);
--
Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/