Re: Xpath query of XPathResult?
Axel Hecht <[email protected]> Wed, 07 Jun 2006 19:56:54 +0200
| Newsgroups | gmane.comp.mozilla.devel.xml |
|---|---|
| Message-ID | <[email protected]> |
hairbo wrote:
> Okay, I've got an answer.
>
> First off, I was wrong about the result of "selectNodes". In IE, it
> returns an array of XML elements that cannot be re-queried by Xpath.
> The same is true of Mozilla, so while the syntax is different in the
> two browsers, the results are the same.
>
> My goal *is* to be able to re-query the returned elements with Xpath,
> and in order to do that, I need to turn the returned xml element back
> into an xml document.
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.
>
<...>