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.
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.
hairbo wrote:
> The need is because I want to be able to grab arbitrary XML via Xpath
> in one section of code, and then requery that subset via Xpath in
> another, unrelated section of code. You're right that if all the code
> was together in one function, this would be unnecessary, but the code
> is disconnected. I also want to have just one way of accessing any Xml
> client-side, and the way I want to do it is with Xpath.
>
>
>
> Martin Honnen wrote:
> > hairbo wrote:
> >
> > > As for Mozilla, I found a workaround that isn't too ugly. I do the
> > > following:
> > >
> > > var xmlString = new
> > > XMLSerializer().serializeToString(result.iterateNext());
> > > var xmlDoc = new DOMParser().parseFromString(xmlString, "text/xml");
> > >
> > > The first line takes the result of the XpathResult ("result"), and
> > > serializes it to a string. The next line takes that string of XML and
> > > converts it into an XmlDocument, which I can then re-query.
> >
> > I am not sure I understand the need for that approach. The iterateNext
> > function gives you a DOM node, if you want to make that DOM node the
> > context node of another XPath evaluation then you can simply do
> > var node = result.iterateNext();
> > if (node != null) {
> > var secondResult = node.ownerDocument.evaluate(
> > 'xpathexpression goes here',
> > node,
> > null,
> > 0,
> > null
> > );
> >
> > --
> >
> > Martin Honnen
> > http://JavaScript.FAQTs.com/
|