ajax - Consuming .net web services from FireFox

  • Thread starter Thread starter Aidy
  • Start date Start date
A

Aidy

I couldn't find an ajax type group so I'll post here. It's a bit OT so feel
free to suggest a better group or web forum.

I've condensed the code below to illustrate the problem I'm having. I've
using the XPathEvaluator to query the returned XML The issue is that the
web service adds a namespace (xmlns="http://tempuri.org/) to the root node
and I can't get my code to work with it. The code below has two lines where
we set the text, one with the namesapce and one without. It works without
the namespace but not with. I think the solution is to do with the third
param of the evaluate method but I'm at a loss. Any ideas?

var parser=new DOMParser();

//var text = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ArrayOfWord
xmlns:xi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns=\"http://tempuri.org/\"><Word><ID>1</ID><Value>A</Value></Word><Word><ID>2</ID><Value>B</Value></Word></ArrayOfWord>";

var text = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ArrayOfWord
xmlns:xi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Word><ID>1</ID><Value>A</Value></Word><Word><ID>2</ID><Value>B</Value></Word></ArrayOfWord>";

var XmlDOM=parser.parseFromString(text,"text/xml");
var oEval = new XPathEvaluator();
var oResult = oEval.evaluate ('Word/Value', XmlDOM.documentElement, null, 0,
null);
if (oResult != null)
{
xel = oResult.iterateNext();
while (xel)
{
alert (xel.textContent);
xel = oResult.iterateNext();
}
}
 
firefox follows the w3c standard for namespace support in xpath queries.
you need to supply a namespace resolver object that you pass to the
evaluate. something like:

var oResult = oEval.evaluate (
'Word/Value',
XmlDOM.documentElement,
{
normalResolver: XmlDom.createNSResolver(XmlDom.documentElement),
lookupNamespaceURI : function(p) {
switch(p) {
case 'xi;:
return "http://www.w3.org/2001/XMLSchema-instance";
case 'xsd':
return "http://www.w3.org/2001/XMLSchema";
default:
return this.normalResolver.lookupNamespaceURI(p);
}
}
},
0,
null);


your other option is to remove the namespace specs before loading the dom.

-- bruce (sqlwork.com)
 
Back
Top