This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Mozilla XML > May 2005 > Node.prototype.selectNodes function problems
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Node.prototype.selectNodes function problems
|
|
| b.dam@gmx.net 2005-05-17, 4:22 am |
| I'm trying to create a javascript application that uses data returned
from a (ASP.NET) webservice. I want a cross-browser (IE/Mozilla)
solution for this. The webservice data loads fine into a document, but
then I want to use XPath to select particular nodes in this document.
IE has a selectNodes function for this, so I want to create this
function for Mozilla as well. I have the following for Mozilla:
Node.prototype.selectNodes = function(sExpr)
{
//DOCUMENT_NODE = 9
var doc = this.nodeType == 9 ? this : this.ownerDocument;
var nsRes = doc.createNSResolver(this.nodeType == 9 ?
this.documentElement : this);
var xpRes = doc.evaluate(sExpr, this, nsRes, 5, null);
var res = [];
var item;
while (item = xpRes.iterateNext())
res.push(item);
return res;
}
When I use the Mozilla javascript debugger on this piece of code and
the doc.evaluate statement has been executed, the xpRes variable
contains a lot of XPathExceptions, with the following value: "The
expression cannot be converted to return the specified type."
Any help appreciated
| |
| Emmanouil Batsis 2005-05-17, 7:54 am |
| b.dam@gmx.net wrote:
> I'm trying to create a javascript application that uses data returned
> from a (ASP.NET) webservice. I want a cross-browser (IE/Mozilla)
> solution for this. The webservice data loads fine into a document, but
> then I want to use XPath to select particular nodes in this document.
Try the sarissa library at sarissa.sf.net, it implements selectNodes and
selectSingleNode for Moz (or any DOM L3 Xpath capable browser)
hth,
Manos
| |
| b.dam@gmx.net 2005-05-17, 7:54 am |
|
Yes, I know of Sarissa, it's just a little overkill for my app, I want
to keep it as lightweight as possible.
I think my problem has something to do with the namespaces in my
document. Do I have to do something special to make the
document.evaluate function work with an xmldocument with soap
namespaces (soap, xsi, xsd)?
| |
| Emmanouil Batsis 2005-05-17, 7:54 am |
| b.dam@gmx.net wrote:
> Yes, I know of Sarissa, it's just a little overkill for my app, I want
> to keep it as lightweight as possible.
>
> I think my problem has something to do with the namespaces in my
> document. Do I have to do something special to make the
> document.evaluate function work with an xmldocument with soap
> namespaces (soap, xsi, xsd)?
>
Yup, you have to bind namespaces URIs to prefixes used in your XPath see
how sarissa does it.
| |
| b.dam@gmx.net 2005-05-17, 7:54 am |
| Okay, I've studied sarissa, it needs an array with namespaces and a
binding function. I've tried this, in a quick and dirty solution, but
the code below still doesn't work:
Node.prototype.selectNodes =3D function(sExpr)
{
//DOCUMENT_NODE =3D 9
var doc =3D this.nodeType =3D=3D 9 ? this : this.ownerDocument;
var nsRes =3D doc.createNSResolver(this.node=ADType =3D=3D 9 ?
this.documentElement : this);
doc._namespaces =3D {};
doc._namespaces["soap"] =3D
"http://schemas.xmlsoap.org/soap/envelope/";
doc._namespaces["xsi"] =3D "http://www.w3.org/2001/XMLSchema-instance";
doc._namespaces["xsd"] =3D "http://www.w3.org/2001/XMLSchema";
var nsRes2 =3D function(s)
{
return doc._namespaces[s];
}
var xpRes =3D doc.evaluate(sExpr, this, nsRes2, 5, null);
var res =3D [];
var item;
while (item =3D xpRes.iterateNext())
res.push(item);=20
return res;=20
}
| |
| Martin Honnen 2005-05-17, 7:40 pm |
|
b.dam@gmx.net wrote:
> I'm trying to create a javascript application that uses data returned
> from a (ASP.NET) webservice. I want a cross-browser (IE/Mozilla)
> solution for this. The webservice data loads fine into a document, but
> then I want to use XPath to select particular nodes in this document.
> IE has a selectNodes function for this, so I want to create this
> function for Mozilla as well. I have the following for Mozilla:
>
> Node.prototype.selectNodes = function(sExpr)
> {
> //DOCUMENT_NODE = 9
> var doc = this.nodeType == 9 ? this : this.ownerDocument;
>
> var nsRes = doc.createNSResolver(this.nodeType == 9 ?
> this.documentElement : this);
>
> var xpRes = doc.evaluate(sExpr, this, nsRes, 5, null);
>
> var res = [];
> var item;
> while (item = xpRes.iterateNext())
> res.push(item);
> return res;
> }
>
> When I use the Mozilla javascript debugger on this piece of code and
> the doc.evaluate statement has been executed, the xpRes variable
> contains a lot of XPathExceptions, with the following value: "The
> expression cannot be converted to return the specified type."
How does the XPath expression look, how does the XML document look,
which node are you calling the method on?
--
Martin Honnen
http://JavaScript.FAQTs.com/
| |
| b.dam@gmx.net 2005-05-17, 7:40 pm |
| I'm calling the method on the document itself. It's a pretty standard
soap document, with above mentioned namespaces and a default namespace
of "xmlns='http://myservice.com'", and I'm using an XPathExpression of
"//Mastercolumn". I'm sorry I can't paste the document here, for
testing purposes, but the code is on my laptop which isn't connected to
inet or the network.
Am going into the right direction with that array that I made?
| |
| Martin Honnen 2005-05-17, 7:40 pm |
|
b.dam@gmx.net wrote:
> I'm calling the method on the document itself. It's a pretty standard
> soap document, with above mentioned namespaces and a default namespace
> of "xmlns='http://myservice.com'", and I'm using an XPathExpression of
> "//Mastercolumn". I'm sorry I can't paste the document here, for
> testing purposes, but the code is on my laptop which isn't connected to
> inet or the network.
>
> Am going into the right direction with that array that I made?
If you have a default namespace then you need to bind a prefix to it to
have an XPath expression selecting elements in that default namespace, see
<http://www.faqts.com/knowledge_base...d/34022/fid/616>
--
Martin Honnen
http://JavaScript.FAQTs.com/
| |
|
| b.dam@gmx.net wrote:
> Okay, I've studied sarissa, it needs an array with namespaces and a
> binding function. I've tried this, in a quick and dirty solution, but
> the code below still doesn't work:
>
> Node.prototype.selectNodes = function(sExpr)
> {
> //DOCUMENT_NODE = 9
> var doc = this.nodeType == 9 ? this : this.ownerDocument;
>
> var nsRes = doc.createNSResolver(this.node_Type == 9 ?
> this.documentElement : this);
>
> doc._namespaces = {};
> doc._namespaces["soap"] =
> "http://schemas.xmlsoap.org/soap/envelope/";
> doc._namespaces["xsi"] = "http://www.w3.org/2001/XMLSchema-instance";
> doc._namespaces["xsd"] = "http://www.w3.org/2001/XMLSchema";
>
> var nsRes2 = function(s)
> {
> return doc._namespaces[s];
> }
>
> var xpRes = doc.evaluate(sExpr, this, nsRes2, 5, null);
>
> var res = [];
> var item;
> while (item = xpRes.iterateNext())
> res.push(item);
> return res;
> }
>
// perhaps this could help:
Node.prototype.namespaceResolver = function(prefix) {
swith(prefix){
case 'soap': return 'http://schemas.xmlsoap.org/soap/envelope/';
case 'xsi': return 'http://www.w3.org/2001/XMLSchema-instance';
case 'xsd': return 'http://www.w3.org/2001/XMLSchema';
case 'xul': return
'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
}
return null;
}
Document.prototype.selectNodes =
Node.prototype.selectNodes = function(xpath) {
var nodes = document.evaluate(xpath,this,this.namespaceResolver,0,null);
var nodelist = new Array();
var node = nodes.iterateNext();
while( node ){nodelist.push( node );node = nodes.iterateNext();}
return nodelist;
}
| |
| b.dam@gmx.net 2005-05-18, 4:37 am |
| Well, I've finally got it working, with the code below. It only works
though when I use a prefix in my XPath expression, for example
"//ws:MasterColumn". Can I use XPath in mozilla without using that
prefix? I'm creating a cross-browser solution, and XPath expressions
with prefixes don't work in IE.
Node.prototype.selectNodes =3D function(sExpr)
{
//DOCUMENT_NODE =3D 9
var doc =3D this.nodeType =3D=3D 9 ? this : this.ownerDocument;
var nsRes =3D doc.createNSResolver(this.node=ADType =3D=3D 9 ?
this.documentElement : this);
var nsRes2 =3D function(s) {
return "http://myservice.com";
}
var xpRes =3D doc.evaluate(sExpr, this, nsRes2, 5, null);
var res =3D [];
var item;
while (item =3D xpRes.iterateNext())
res.push(item);=20
return res;
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|