Re: Xpath query of XPathResult?
"hairbo" <[email protected]> 8 Jun 2006 14:26:19 -0700
| Newsgroups | gmane.comp.mozilla.devel.xml |
|---|---|
| Organization | http://groups.google.com |
| Message-ID | <[email protected]> |
Martin,
First off, thanks for responding.
Secondly, I discovered Opera 9 beta last night, tested my code against
it, and am pleased to say that it works.
Finally, you're right again. I didn't need to convert everything into
xml documents again. The crucial piece I was missing was the
"ownerDocument" piece. If I run evaluate() against an xml document,
then I don't need that "ownerDocument" property, but if I evaluate
against an element of that document, then I do need th ownerDocument
property.
The relevant Gecko-flavored code for me now looks like this:
function getnode(node,xpath){
if(node.ownerDocument){
var thisnode = node.ownerDocument;
var nsResolver =
document.createNSResolver(node.ownerDocument.documentElement);
}else{
var thisnode = node;
var nsResolver = document.createNSResolver(node.documentElement);
}
return thisnode.evaluate(xpath, node, nsResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}
Thanks for your help. I *think* I'm done now.
Ben
Martin Honnen wrote:
> hairbo wrote:
>
> > 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.
>
> You get a DOM node list not a JavaScript array. And right, you can't do
> an XPath query on the complete node list. But as pointed out, you can
> loop through the list and call selectNodes (or selectSingleNode) on each
> node so that way you can requery on each node.
>
> > The same is true of Mozilla, so while the syntax is different in the
> > two browsers, the results are the same.
>
> Mozilla's XPath API gives you a node set in various form, either as a
> snapshot or as an iterator. Again you can't do an XPath on the complete
> result returned but you can certainly pass in each node you iterate over
> or the snapshot gives into the evaluate method as as the second argument.
>
> > 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.
>
> I don't understand that approach at all, why can't you call selectNodes
> on each node returned or use the evaluate method to pass in each node as
> the context node?
> That is possible and makes much more sense in general as your attempt to
> convert a node back into a document blows up if you tried that with a
> text node or attribute node or comment node or processing instruction node.
>
>
>
> > 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.
>
> Opera 9 beta follows Mozilla to implement the W3C DOM Level 3 XPath API
> so with Opera 9 there will be XPath support.
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/