Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-04-2010, 11:13 AM   PM User | #1
vcage
New to the CF scene

 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vcage is an unknown quantity at this point
Using xpath for an xml with xmlns defined

Hello,
I want to read tag values of an xml using xpath in firefox extension (basically javascript). The example from w3schools works perfectly fine as long as the XML does not have XMLNS attribute to the root element. The same example code does not work with xmlns attribute specified.

Similarly, the xml I receive from my SOAP service has xmlns as below:

<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2009-07-15/">

However, I am unable to read values from my xml, inspite of specifying a 'name space resolver'. I have tried with simple namespace resolver which just returns the above URI as a string and also tried creating NSResolver following the instructions given here.

Could anyone help me how to solve this?

Regards,
vcage
vcage is offline   Reply With Quote
Old 03-04-2010, 12:21 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,860
Thanks: 9
Thanked 290 Times in 286 Posts
Dormilich is on a distinguished road
maybe MDC can help, from glancing over the text, it might be what you need (esp. the later examples)
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 03-04-2010, 12:52 PM   PM User | #3
vcage
New to the CF scene

 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vcage is an unknown quantity at this point
Hi Dormilich,

I have already followed the guide lines given at the above link and created the nsresolver. It didn't help.

Anyway, thanks for the response.

Regards,
vcage
vcage is offline   Reply With Quote
Old 03-04-2010, 01:18 PM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,860
Thanks: 9
Thanked 290 Times in 286 Posts
Dormilich is on a distinguished road
can I see your code, maybe I can spot something …
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 03-04-2010, 01:35 PM   PM User | #5
vcage
New to the CF scene

 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vcage is an unknown quantity at this point
Arrow

Hi,
Thanks for your response again. Here is my code.. This is w3schools example tweaked.

I have created the resolver as suggested by MDC. If I manually modify the xmlns attribute to contain a prefix like "xmlns:xyz" everything works fine without a resolver. But my SOAP service doesnot return any prefix by default.

Could you help with the right approach to handle this scenario?
Many thanks in advance,
Regards,
vcage

Code:
<html>
<body>
<script type="text/javascript">

function loadXMLDoc(dname)
{

  xhttp=new XMLHttpRequest();
  xhttp.open("GET",dname,false);
  xhttp.send("");
  return xhttp.responseXML;
}

// load the xml
xml=loadXMLDoc("book.xml");

path="/bookstore/book/title";

// Steps suggested by mozilla MDC
var evaluator = new XPathEvaluator();
var resolver = evaluator.createNSResolver(xml.documentElement);

var nodes=evaluator.evaluate(path, xml.documentElement, resolver, XPathResult.ANY_TYPE, null);

var result=nodes.iterateNext();

while (result)
{
  document.write(result.childNodes[0].nodeValue);
  document.write("<br />");
  result=nodes.iterateNext();
}

</script>

</body>
</html>
vcage is offline   Reply With Quote
Old 03-05-2010, 06:34 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,860
Thanks: 9
Thanked 290 Times in 286 Posts
Dormilich is on a distinguished road
I think that’s the wrong resolver, from what I read, you need a custom resolver (examples quite at the end of the page)
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
xmlns, xpath

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:22 PM.


Advertisement
Log in to turn off these ads.