This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Mozilla XML > May 2005 > XPath expression prefixes





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 XPath expression prefixes
b.dam@gmx.net

2005-05-18, 8:26 am

<<Continued from yesterday>>

I finally have my selectNodes function working. The only problem is
that I always have to use a prefix in my XPath expression, for example
"//ws:MasterRow". This prefix causes problems in IE however. Can I use
XPath in Mozilla on a Soap document without using prefixes in my XPath
expression?

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 ;=20
}

Martin Honnen

2005-05-18, 8:26 am



b.dam@gmx.net wrote:

> <<Continued from yesterday>>
>
> I finally have my selectNodes function working. The only problem is
> that I always have to use a prefix in my XPath expression, for example
> "//ws:MasterRow". This prefix causes problems in IE however.


That sounds more like you are not using XPath in IE, make sure you are
using IE 6 or you use IE 5/5.5 with MSXML 3 or later and you call
xmlDocument.setProperty('SelectionLanguage', 'XPath')
before you use selectNodes/selectSingleNode as otherwise you are not
using XPath 1.0 but some other selection language that MS implemented
before they implemented the W3C recommendation XPath 1.0.
And of course with MSXML if you want to use prefixes in XPath expression
you have to declare them e.g.
xmlDocument.setProperty('SelectionNamespaces',
'xmlns:pf1="http://example.com/ns1" xmlns:pf2="http://example.org/ns"');

> Can I use
> XPath in Mozilla on a Soap document without using prefixes in my XPath
> expression?


Sure
//*
for instance or
//@*
will work but of course if you want to match elements in a namespace
then you need to use prefixes or inefficient workarounds like
//*[local-name() = 'Mastercolumn']


--

Martin Honnen
http://JavaScript.FAQTs.com/
Lucky

2005-05-19, 4:27 am

b.dam@gmx.net wrote:
> <<Continued from yesterday>>
>
> I finally have my selectNodes function working. The only problem is
> that I always have to use a prefix in my XPath expression, for example
> "//ws:MasterRow". This prefix causes problems in IE however. Can I use
> XPath in Mozilla on a Soap document without using prefixes in my XPath
> expression?
>
> 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);
>
> var nsRes2 = function(s) {
> return "http://myservice.com";
> }
>
> var xpRes = doc.evaluate(sExpr, this, nsRes2, 5, null);
> var res = [];
> var item;
> while (item = xpRes.iterateNext())
> res.push (item);
> return res ;
> }
>

this is probably what you are seeking..
http://teamcon.com/pub/concepts/moz...selectnodes.htm

please feel free to test this out as much as possible..
this is an updated portion of my cross-browser library :)


primary focus would be in the 3) + 4) samples..
they show how .protype(s) can be used to get a fuller IE model support

xmlDocument.setPropery('SelectionNamespaces','blah blah');

the XMLDocument.globalNamespaces is a nice concept as well.
but you would need to add them consistently for IE or
if you have a consistent loadXML( ), you could apply them there.
simply

xmldom.setProperty('SelectionNamespaces',XMLDocument.globalNamespaces.toString());

to get the xmlns:foo="bar" entries.

oh yea, and finally.. this version has not been marked up to allow for
FULL cross browser support right now.
it has been marked up and focus was only on Moz/Fx browsers.
so you'd have to add in code to wire up a mock XMLDocument {} for IE, etc.

g'luck

b.dam@gmx.net

2005-05-19, 4:27 am

@Martin
Thanks, you've helped me in the right direction on this one. It works
like a charm now.

@Lucky
I get a "DOMParser is undefined" error on opening your page and it
shows just a blank screen after that.

Lucky

2005-05-19, 7:31 pm

b.dam@gmx.net wrote:
> @Martin
> Thanks, you've helped me in the right direction on this one. It works
> like a charm now.
>
> @Lucky
> I get a "DOMParser is undefined" error on opening your page and it
> shows just a blank screen after that.
>

in my message, i noted that this copy was not intended for IE.
it was intended to show mozilla specific portions of the code.

feel free to try this new copy, that has less comments, but works
fluidly for both browsers.

http://www.teamcon.com/pub/concepts...selectNodes.htm
Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews