Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    maybe MDC can help, from glancing over the text, it might be what you need (esp. the later examples)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    can I see your code, maybe I can spot something …
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    I think that’s the wrong resolver, from what I read, you need a custom resolver (examples quite at the end of the page)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •