Xpath query of XPathResult?

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Xpath query of XPathResult?

hairbo
Not sure if this is the best place for this question, so apologies if
it is not.

I would like to be able to run an Xpath query on the results returned
by the XPathResult "evaluate" function.  I have a shell of XML, like
this:

<results>arbitrary results here</results>

...where the "arbitrary results" could be a string, or more XML, such
as:

<results>
 <person>
  <firstname>John</firstname>
  <lastname>Doe</lastname>
 </person>
</results>

If I run an Xpath query to get the contents of the "results" node, I
cannot then run another xpath query against those results.

I suppose the question is this:  is there any way to "reset" the
returned value of "results" from an "XpathResult" back to an
"XmlDocument", or something like it?

The MSXML parser seems to have no difficulty running an xpath query on
a returned nodeset, so I'm struggling to see why this appears to be so
difficult in Mozilla's implementation.  However, I'm fairly new to this
code, so perhaps I'm missing some basic principle here.

Any help would be appreciated.

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

Axel Hecht-2
hairbo wrote:

> Not sure if this is the best place for this question, so apologies if
> it is not.
>
> I would like to be able to run an Xpath query on the results returned
> by the XPathResult "evaluate" function.  I have a shell of XML, like
> this:
>
> <results>arbitrary results here</results>
>
> ...where the "arbitrary results" could be a string, or more XML, such
> as:
>
> <results>
>  <person>
>   <firstname>John</firstname>
>   <lastname>Doe</lastname>
>  </person>
> </results>
>
> If I run an Xpath query to get the contents of the "results" node, I
> cannot then run another xpath query against those results.
>
> I suppose the question is this:  is there any way to "reset" the
> returned value of "results" from an "XpathResult" back to an
> "XmlDocument", or something like it?
>
> The MSXML parser seems to have no difficulty running an xpath query on
> a returned nodeset, so I'm struggling to see why this appears to be so
> difficult in Mozilla's implementation.  However, I'm fairly new to this
> code, so perhaps I'm missing some basic principle here.
>
> Any help would be appreciated.
>

I don't see why you shouldn't be able to do that, other than that we
don't take an XpathResult as argument to evaluate, but only dom nodes.
So you had to make that extra step and iterate over the result, get the
node, and then evaluate with that.

Not sweet, but works.

E4X allows you do to the thing you want directly, btw, but of course,
that's gecko only for now.

Axel
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

hairbo
Hi Axel,

Thanks for the response.  I think I sort of get what you're suggesting,
but what if the results XML looks like this:

<results>
   <firstname>John</firstname>
   <lastname>Doe</lastname>
</results>

You see the difference here between this example and my previous one,
right?  There's no containing "<person>" element, only two sibling
nodes.  In that case if I iterate over the XpathResult, as you suggest,
I'll get each sibling one at a time, rather than both together.  So, in
this case, is what I want to do more or less impossible, or do I need
to build a new XMLDocument on the fly somehow?

Thanks again,
Ben



Axel Hecht wrote:

> hairbo wrote:
> > Not sure if this is the best place for this question, so apologies if
> > it is not.
> >
> > I would like to be able to run an Xpath query on the results returned
> > by the XPathResult "evaluate" function.  I have a shell of XML, like
> > this:
> >
> > <results>arbitrary results here</results>
> >
> > ...where the "arbitrary results" could be a string, or more XML, such
> > as:
> >
> > <results>
> >  <person>
> >   <firstname>John</firstname>
> >   <lastname>Doe</lastname>
> >  </person>
> > </results>
> >
> > If I run an Xpath query to get the contents of the "results" node, I
> > cannot then run another xpath query against those results.
> >
> > I suppose the question is this:  is there any way to "reset" the
> > returned value of "results" from an "XpathResult" back to an
> > "XmlDocument", or something like it?
> >
> > The MSXML parser seems to have no difficulty running an xpath query on
> > a returned nodeset, so I'm struggling to see why this appears to be so
> > difficult in Mozilla's implementation.  However, I'm fairly new to this
> > code, so perhaps I'm missing some basic principle here.
> >
> > Any help would be appreciated.
> >
>
> I don't see why you shouldn't be able to do that, other than that we
> don't take an XpathResult as argument to evaluate, but only dom nodes.
> So you had to make that extra step and iterate over the result, get the
> node, and then evaluate with that.
>
> Not sweet, but works.
>
> E4X allows you do to the thing you want directly, btw, but of course,
> that's gecko only for now.
>
> Axel

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

hairbo
In reply to this post by Axel Hecht-2

Axel Hecht wrote:

> E4X allows you do to the thing you want directly, btw, but of course,
> that's gecko only for now.

I hadn't heard of E4X before now.  Pretty sweet!  Unfortunately, I need
a cross-browser solution that supports XPath, and it appears that E4X
does not support Xpath at this time--a deal-breaker for me.  But a very
promising technology.

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

Axel Hecht
In reply to this post by hairbo
hairbo wrote:

> Hi Axel,
>
> Thanks for the response.  I think I sort of get what you're suggesting,
> but what if the results XML looks like this:
>
> <results>
>    <firstname>John</firstname>
>    <lastname>Doe</lastname>
> </results>
>
> You see the difference here between this example and my previous one,
> right?  There's no containing "<person>" element, only two sibling
> nodes.  In that case if I iterate over the XpathResult, as you suggest,
> I'll get each sibling one at a time, rather than both together.  So, in
> this case, is what I want to do more or less impossible, or do I need
> to build a new XMLDocument on the fly somehow?
>

Well, you can still do it, the only problem and significant difference
is that you loose the context nodeset for the second xpath expression.

If that's what's bothering you, sorry, no solution for that.

Axel

>
> Axel Hecht wrote:
>> hairbo wrote:
>>> Not sure if this is the best place for this question, so apologies if
>>> it is not.
>>>
>>> I would like to be able to run an Xpath query on the results returned
>>> by the XPathResult "evaluate" function.  I have a shell of XML, like
>>> this:
>>>
>>> <results>arbitrary results here</results>
>>>
>>> ...where the "arbitrary results" could be a string, or more XML, such
>>> as:
>>>
>>> <results>
>>>  <person>
>>>   <firstname>John</firstname>
>>>   <lastname>Doe</lastname>
>>>  </person>
>>> </results>
>>>
>>> If I run an Xpath query to get the contents of the "results" node, I
>>> cannot then run another xpath query against those results.
>>>
>>> I suppose the question is this:  is there any way to "reset" the
>>> returned value of "results" from an "XpathResult" back to an
>>> "XmlDocument", or something like it?
>>>
>>> The MSXML parser seems to have no difficulty running an xpath query on
>>> a returned nodeset, so I'm struggling to see why this appears to be so
>>> difficult in Mozilla's implementation.  However, I'm fairly new to this
>>> code, so perhaps I'm missing some basic principle here.
>>>
>>> Any help would be appreciated.
>>>
>> I don't see why you shouldn't be able to do that, other than that we
>> don't take an XpathResult as argument to evaluate, but only dom nodes.
>> So you had to make that extra step and iterate over the result, get the
>> node, and then evaluate with that.
>>
>> Not sweet, but works.
>>
>> E4X allows you do to the thing you want directly, btw, but of course,
>> that's gecko only for now.
>>
>> Axel
>
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

Martin Honnen-3
In reply to this post by hairbo
hairbo wrote:


> The MSXML parser seems to have no difficulty running an xpath query on
> a returned nodeset, so I'm struggling to see why this appears to be so
> difficult in Mozilla's implementation.  

Could you show what kind of MSXML code use you have?
In my understanding you can call selectNodes/selectSingleNode with a
single node as the context node. I don't know of a way to run an XPath
on a complete nodeset.
And with Mozilla's XPath API it is possible to pass in a single node as
the context node too so I am not sure what you think MSXML can do that
Mozilla's API can't do.


--

        Martin Honnen
        http://JavaScript.FAQTs.com/
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

hairbo
Martin,

It turns out you're right, at least on the JS side.

I've used MSXML on the server side, and I recall being able to re-query
the returned node with Xpath, but it's possible that somebody else was
doing some kind of manipulation of the returned object to make it
accessible by Xpath again.

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.

The only issue for me is that the "serializeToString" function returns
that containing node, when I really only want the children.  So, I'll
need to strip away the containing node before calling
"parseFromString".

This all may seem a bit ugly, but this code is intended to live under
the hood, and I really find Xpath the most elegant and simple way of
accessing nodes in XML, so the under-the-hood code ends up fairly ugly,
but the code accessing these functions ends up pretty clean.



Martin Honnen wrote:

> hairbo wrote:
>
>
> > The MSXML parser seems to have no difficulty running an xpath query on
> > a returned nodeset, so I'm struggling to see why this appears to be so
> > difficult in Mozilla's implementation.
>
> Could you show what kind of MSXML code use you have?
> In my understanding you can call selectNodes/selectSingleNode with a
> single node as the context node. I don't know of a way to run an XPath
> on a complete nodeset.
> And with Mozilla's XPath API it is possible to pass in a single node as
> the context node too so I am not sure what you think MSXML can do that
> Mozilla's API can't do.
>
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

Martin Honnen-3
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/
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

hairbo
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/

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

hairbo
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/

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

Axel Hecht
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.
>
<...>
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

hairbo
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.
> >
> <...>

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

Martin Honnen-3
In reply to this post by hairbo
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/
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Xpath query of XPathResult?

hairbo
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/

_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml