Re: document.evaluate
"Leon Bezuidenhout" <[email protected]> Thu, 21 Dec 2006 10:19:49 +0200
| Newsgroups | gmane.comp.mozilla.devel.xml |
|---|---|
| Message-ID | <[email protected]> |
Hi,
Thanks for the response. I found a work around by loading the XML island
into an XMLDocument using the DOMParser as follows:
Element.prototype.selectSingleNode = function(cXPathString, xNode)
{
//create a DOMParser
var objDOMParser = new DOMParser();
if( !xNode ) { xNode = this; }
if(xNode.innerHTML){
var oXML = objDOMParser.parseFromString(xNode.innerHTML, "text/xml");
}
else{
var oXML = objDOMParser.parseFromString(xNode.xml, "text/xml");
}
dump(oXML.documentElement.nodeName == "parsererror" ? "error while parsing"
: oXML.documentElement.nodeName);
var xItems = this.selectNodes(cXPathString, oXML);
if(xItems.length > 0){
return xItems[0];
}
else{
return null;
}
}
"Jonas Sicking" <[email protected]> wrote in message
news:[email protected]...
> Leon Bezuidenhout wrote:
>> I have a page with a couple of XML islands. I call the following code to
>> return a specific node in one of the XML islands
>> document.evaluate(cXPathString, xNode, oNSResolver,
>> XPathResult.FIRST_ORDERED_NODE_TYPE, null);
>> where xNode is a reference to the XML island I want to query. However, my
>> result is always in context of the first XML island on the form
>>
>> Is this a bug or am I doing something wrong?
>
> The issue is probably with the "XML island" rather than the XPath query.
> Firefox does not support XML islands in HTML pages, the whole page will be
> parsed using HTML parsing rules which may not produce the result you want.
> Use the DOM-Inspector to verify that the DOM looks like what you expect.
>
> Alternatively, the problem could be in your XPath query. Note that the
> contents in your "XML island" is part of the normal page, so if you
> evaluate an expression like "//foo" you'll get all foo elements in the
> entire document, not just in that "XML island". And since you're
> requesting just the first node that node would likely be in the first
> island.
>
> Hard to know what else could be the problem without an actual testcase.
>
> / Jonas