Re: Xpath query of XPathResult?

"hairbo" <[email protected]> 7 Jun 2006 14:15:50 -0700
Newsgroups gmane.comp.mozilla.devel.xml
Organization http://groups.google.com
Message-ID <[email protected]>
I think you're right, but I still don't think it'll work for what I
want to do, since I want to make calls to my XmlHttpRequest/Xpath
function set without worrying about what browser I'm in.  So, for
example, if I have an xml node set passed back from an XmlHttpRequest,
I want to be able to do the following in my external code:

for(var i=0; i<nodes.length; i++){

res = doXpathQuery(nodes[i],"./some/xpath/here");

}

I think, given this desire, "nodes" must be a JS array, and therefore I
need to take the ugly step I'm taking.  I'd be happy to be wrong on
this point, but I think since the result of Mozilla's "evaluate()"
statement isn't a JS array (or, at least, it doesn't act like one),
then i need to do what I'm doing.

I swear I've tried running both "selectNodes()" in IE and "evalute()"
in Mozilla on the each item in the returned Xpath results, and every
time it has bombed out.  The only way it has worked for me is to
convert the returned results back into an XmlDocument.  But I
dunno...maybe I"m on crack or something.

And of course...none of this works in Safari, since it doesn't appear
to support Xpath right now.




Axel Hecht wrote:
> No. At least for mozilla, you just don't have to do that.
>
> Martin gave you an example. I don't know about the IE api, though.
>
> Maybe there was indeed a good reason for not using IE's stuff after all ;-).
>
> OK, this only holds true if you don't have to abstract between real
> documents and document parts.
>
> I wonder if anybody wrote an E4X compat layer for IE, or at least
> something that does something that generally can be written like E4X.
> Cause that's the API you're looking for, really.
>
> Axel
>
>
> > The only way I've found to do this is to loop over the returned arrays,
> > serialize the element in each array position to an xml string, turn
> > that string into an xml document, and then add that xml document to a
> > new array.
> >
> > In IE, the code to do that is:
> >
> > var xpathresult = node.selectNodes(xpath);
> > var newnodes = new Array();
> >
> > if(xpathresult.length > 0){
> > 				for(var i=0; i<xpathresult.length; i++){
> > 					var xmlstring = xpathresult[i].xml;
> > 					var xmldoc=new ActiveXObject("Microsoft.XMLDOM");
> > 					xmldoc.async="false";
> > 					xmldoc.loadXML(xmlstring);
> > 					newnodes[newnodes.length] = xmldoc;
> > 				}
> > 			}
> >
> >
> > In Mozilla, the code is:
> >
> > var xpathresult = node.evaluate(xpath, node, nsResolver,
> > XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
> >
> > var newnodes = new Array();
> >
> > for ( var i=0 ; i < res.count; i++ ){
> > var xmlstring = new
> > XMLSerializer().serializeToString(xpathresult.snapshotItem(i));
> > var xmldoc = new DOMParser().parseFromString(xmlstring, "text/xml");
> > newnodes[newnodes.length] = (xmldoc);
> > }
> >
> >
> > Tortured though this approach may seem, it works well, and I find it
> > easiest to use Xpath to access Xml.
> >
> > The only trouble is, I think, Safari and Opera do not offer Xpath
> > support at this time.  Or, at the very least, I can find no evidence
> > that they do.  So that's a bummer.
> > 
> <...>