locked
Querying the xml with namespace but no prefix RRS feed

  • Question

  • I've below xml. 

     

    <?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetDateTimeResponse xmlns='http://aunch'><GetDateTimeResult>2011-06-17T13:01:49.487</GetDateTimeResult></GetDateTimeResponse></soap:Body></soap:Envelope>

     

    but the xpath /soap:Envelope/soap:Body/GetDateTimeResponse/GetDateTimeResult doesn't return any value.

     

    little bit of history here:

    I've got it working by stripping namespaces. Works absolutely fine.

    But I also need to get it working without stripping the namespaces. 

    I#m getting all the namespaces dynamically and then tried to append that to namespacemanager while calling navigator.select("xpath",namespacemanager)

     

    it returns null. What am I missing here? is it to do with prefix?

     

    Please help

     

    Kavya


    Peace, Kavya
    • Edited by Kavya_Shiva Wednesday, June 22, 2011 12:22 PM
    Wednesday, June 22, 2011 11:04 AM

Answers

  • Sorry, I don't understand your latest post. With XPath (1.0 at least) the path "foo" select elements with local name "foo" in no namespace. If you have elements in a namespace then you need to bind a prefix to the namespace uri and use that prefix to select elements in that namespace. Or you need to write stuff like "*[local-name() = 'foo']".

     


    MVP Data Platform Development My blog
    • Marked as answer by Kavya_Shiva Wednesday, June 22, 2011 1:07 PM
    Wednesday, June 22, 2011 12:51 PM

All replies

  • You can query the XML as it is but in your code with your XmlNamespaceManager you need to bind a prefix to the default namespace in the XML (e.g.

      XmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable);

      mgr.AddNamespace("il", "http://www.tribold.com/integration/launch");

      // add other namespaces here

    ) then you need to use that prefix in your XPath expressions

      /soap:Envelope/soap:Body/il:GetDateTimeResponse/il:GetDateTimeResult


    MVP Data Platform Development My blog
    Wednesday, June 22, 2011 12:19 PM
  • You are correct.

    That works. But the scenario in my case is that the xml and xpath are sent from else where. which means, I wouldn't know where in xpath to add the prefix. Will end up doing regex, and what not

    It would get very tricky wouldn't?

     

     

    XML is correct, xpath is correct. what's the reason behind this behaviour?

     

    Kavya


    Peace, Kavya
    Wednesday, June 22, 2011 12:22 PM
  • Sorry, I don't understand your latest post. With XPath (1.0 at least) the path "foo" select elements with local name "foo" in no namespace. If you have elements in a namespace then you need to bind a prefix to the namespace uri and use that prefix to select elements in that namespace. Or you need to write stuff like "*[local-name() = 'foo']".

     


    MVP Data Platform Development My blog
    • Marked as answer by Kavya_Shiva Wednesday, June 22, 2011 1:07 PM
    Wednesday, June 22, 2011 12:51 PM
  • You're a star. It seems to me that's what i was missing.

    Basically, what I meant in earlier post was that I've no control over xpath and xml file. I was trying to strip off that particular namespace which didn't have prefix. (Should've thought MS would've exposed a function on it)

    I wanted to get it working with or without namespaces.

    You saved my time, Local-name is exactly what I was looking for.

     

    Thanks a ton

     

    Kavya


    Peace, Kavya
    Wednesday, June 22, 2011 1:07 PM