Thursday, April 25, 2013

sudzC SOAP web service namespace problem



I am using sudzC to generate objective-C codes to use SOAP webservice of a Java Tomcat server.
But is doesn't work. There must be some formatting problem.


<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://example.com/"><soap:Body>
<getMethod>
    <arg0>aaa</arg0>
    <arg1>8</arg1>
</getMethod>
</soap:Body></soap:Envelope>


return error message:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
<soap:Fault><faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"http://example.com/", local:"arg0"). Expected elements are &lt;{}arg0&gt;,&lt;{}arg1&gt; </faultstring></soap:Fault>
</soap:Body></soap:Envelope>

server is expecting <{}arg0>, <{}arg1>
{} means the namespace is empty.
arg0 and arg1 should have NO namespace.


The above request uses a default namespace. [2]
The default namespace will apply to all unprefixed elements, which is unwanted.


Then I removed the default namespace.
But the server is expecting namespace for "getMethod"

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode>
<faultstring>Unexpected wrapper element getMethod found.   Expected {http://example.com/} getMethod.</faultstring>
</soap:Fault></soap:Body></soap:Envelope>


So, "getMethod" should have namespace, but arg0 and arg1 should not have namespace. The direct child of "Body" must have namespace. [1]

The following request is OK

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ><soap:Body>
<ws:getMethod xmlns:ws="http://example.com/">
    <arg0>aaa</arg0>
    <arg1>8</arg1>
</ws:getMethod>
</soap:Body></soap:Envelope>

Some more experiments

(1) Is the xmlns:ws will auto apply to the getMethod node. 
I try the following request. The result is "NO".

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" >
<soap:Body>
<getMethod xmlns:ws="http://example.com/">
    <arg0>aaa</arg0>
    <arg >8</arg1>
</getMethod>
</soap:Body>
</soap:Envelope>

returns:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode>
<faultstring>Unexpected wrapper element getMethod found.   Expected {http://example.com/}getMethod.</faultstring></soap:Fault></soap:Body></soap:Envelope>

An example namespace declaration, which associates the namespace prefix edi with the namespace name http://ecommerce.example.org/schema:
The namespace is NOT applied to the elements. But we can use edi to represent the namespace uri. [2]

<x xmlns:edi='http://ecommerce.example.org/schema'>
  <!-- the "edi" prefix is bound to http://ecommerce.example.org/schema
       for the "x" element and contents -->
</x>


(2) some examples on the Internet
uses "soapenv" instead of "soap".  It is OK.

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ><soapenv:Body>
<ws:getMethod xmlns:ws="http://example.com/">
    <arg0>aaa</arg0>
    <arg1>8</arg1>
</ws:getMethod>

</soapenv:Body></soapenv:Envelope>



Reference: 

[1]http://www.w3.org/TR/soap12-part1/#soapbodyel
SOAP Body child Element

[2]http://www.w3.org/TR/REC-xml-names/#ns-decl
Declaring Namespaces

[3]http://www.w3.org/TR/2000/NOTE-SOAP-20000508/
Simple Object Access Protocol (SOAP) 1.1




No comments:

Post a Comment