views:

70

answers:

2

Hi

I have a SOAP message (see below).

Using Xpath, how can I extract the name of the namespace from this message? In other words, is there an Xpath routine that will return the text "validateNewOrder"?

Any suggestions or help would be invaluable. I have been searching everywhere but not found an solution. It is driving me crazy...

Thanks!

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
<SOAP-ENV:Body>
   <ns1:validateNewOrder xmlns:ns1="http://sire.rabobank.nl/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
     <sireheader xmlns="http://sire.rabobank.nl/"&gt;
       <sender>
         <compid>TEST</compid>
       </sender>
     </sireheader>
     <order xmlns="http://sire.rabobank.nl/"&gt;
       <account>123456789</account>
     </order>
   </ns1:validateNewOrder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
A: 
local-name(/*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[1])

Returns the local name of the first element underneath Body in a namespace agnostic way.

Tested it with (c#)

XDocument doc = XDocument.Load(@"XMLFile1.xml");
var xpath = "local-name(/*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[1])";
var res = doc.Root.XPathEvaluate(xpath);
Obalix
Hi ObalixThanks. I tried your suggestion, but it gave a syntax error. So I tried //*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[local-name(*[1])]But that gave me "TEST123456789", not "validateNewOrder". Do you know what I am doing wrong? I am new to SOAP, so am still misunderstanding lots of stuff.Ps - I was using the site http://www.xmlme.com/XpathTool.aspx to test my Xpath queryThanks!Olly
olly
@olly: Ok, there were two errors in the query, fixed them and tested the query with the supplied code. The site you quoted can only process queries that return a node set. So if you want to test whether the right element is selected, just drop the outer `local-name()` and paste the rest into the tool. I.e. `/*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[1]`
Obalix
Hi Obalix. Thanks for the fix. However, when I paste it into the tool, the tool returns "TEST12345689". This is not what I want to retrieve. What I want to be returned is the value "validateNewOrder". Or have I misunderstood your answer. Sorry if my question seems dumb, but I am new to SOAP...
olly
Hi Obelix. You are a genius, and I was being dumb. I tried your query in my Java code and it gives me exactly what I want! Thanks!!!
olly
@olly: then just accept the question, if you want you can up-vote as well :-)
Obalix
A: 

I have my solution, thanks to user Obelix.

The Xpath query that I needed was

local-name(/*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[1])

It does not work on the test site that I was using (apparently, the site I quoted can only process queries that return a node set), but does work in my Java code.

Now I can go out and enjoy the weekend

Olly

olly
@olly: It is not good style to requote a solution someone else provided you with. However, it <b>is</b> good style to accept and/or upvote the person who provided you with the solution and thus reward them for their effort.
Obalix
Sorry Obalix - I have not used this before. I think I have credited your answer to you now. Let me know if it didn't work. I do not have an account yet to try and promote your answer, but when I get that sorted, I will promote your answer.
olly