logo       

Re: Xpath query of XPathResult?: msg#00026

Subject: Re: Xpath query of XPathResult?
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/


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
web.pylons.gene...    hurd.l4/2002-10...    kernel.commits....    user-groups.lin...    yellowdog.gener...    java.drools.use...    security.openva...    package-managem...    linux.debian.us...    qnx.openqnx.dev...    genealogy.gramp...    file-systems.if...    voip.wengophone...    tex.context/200...    ietf.smime/2003...    audio.csound.de...    culture.region....    xfree86.devel/2...    mobile.kannel.u...    distributed.con...    education.engli...    org.user-groups...    bug-tracking.gn...    recreation.bicy...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe