<p>I'm looking at the SOAP output from a web service I'm developing, and I noticed something curious:</p>

<pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"&gt;
   &lt;soapenv:Body&gt;
      &lt;ns1:CreateEntityTypesResponse xmlns:ns1="http://somedomain.com/wsinterface"&gt;
         &lt;newKeys&gt;
            &lt;value&gt;1234&lt;/value&gt;
         &lt;/newKeys&gt;
         &lt;newKeys&gt;
            &lt;value&gt;2345&lt;/value&gt;
         &lt;/newKeys&gt;
         &lt;newKeys&gt;
            &lt;value&gt;3456&lt;/value&gt;
         &lt;/newKeys&gt;
         &lt;newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
         &lt;newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
         &lt;errors&gt;Error1&lt;/errors&gt;
         &lt;errors&gt;Error2&lt;/errors&gt;
      &lt;/ns1:CreateEntityTypesResponse&gt;
   &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;
</code></pre>

<p>I have two newKeys elements that are nil, and both elements insert a namespace reference for xsi.  I'd like to include that namespace in the soapenv:Envelope element so that the namespace reference is only sent once.</p>

<p>I am using WSDL2Java to generate the service skeleton, so I don't directly have access to the Axis2 API.</p>

[{'answer_id': 75128, 'author': 'Michael Sharek', 'author_id': 1958, 'author_profile': 'https://Stackoverflow.com/users/1958', 'pm_score': 3, 'selected': False, 'text': '<h3>Using WSDL2Java</h3>\n\n<p>If you have used the Axis2 WSDL2Java tool you\'re kind of stuck with what it generates for you.  However you can try to change the skeleton in this section:</p>\n\n<pre><code>   // create SOAP envelope with that payload\n   org.apache.axiom.soap.SOAPEnvelope env = null;\n   env = toEnvelope(\n       getFactory(_operationClient.getOptions().getSoapVersionURI()),\n       methodName,\n       optimizeContent(new javax.xml.namespace.QName\n       ("http://tempuri.org/","methodName")));\n\n//adding SOAP soap_headers\n_serviceClient.addHeadersToEnvelope(env);\n</code></pre>\n\n<p>To add the namespace to the envelope add these lines somewhere in there:</p>\n\n<pre><code>OMNamespace xsi = getFactory(_operationClient.getOptions().getSoapVersionURI()).\n    createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");\n\nenv.declareNamespace(xsi);\n</code></pre>\n\n<h3>Hand-coded</h3>\n\n<p>If you are "hand-coding" the service you might do something like this:</p>\n\n<pre><code>SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();   \nSOAPEnvelope envelope = fac.getDefaultEnvelope();\nOMNamespace xsi = fac.createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");\n\nenvelope.declareNamespace(xsi);\nOMNamespace methodNs = fac.createOMNamespace("http://somedomain.com/wsinterface", "ns1");\n\nOMElement method = fac.createOMElement("CreateEntityTypesResponse", methodNs);\n\n//add the newkeys and errors as OMElements here...\n</code></pre>\n\n<h3>Exposing service in aar</h3>\n\n<p>If you are creating a service inside an aar you may be able to influence the SOAP message produced by using the target namespace or schema namespace properties (see <a href="http://wso2.org/library/2060" rel="noreferrer">this article</a>).</p>\n\n<p>Hope that helps.</p>\n'}, {'answer_id': 11162724, 'author': 'dodoconr', 'author_id': 726202, 'author_profile': 'https://Stackoverflow.com/users/726202', 'pm_score': 1, 'selected': False, 'text': '<p>Other option is that the variable MY_QNAME has the prefix empty.</p>\n\n<pre><code>public static final QName MY_QNAME = new QName("http://www.hello.com/Service/",\n            "tagname",\n            "prefix");\n</code></pre>\n\n<p>So, if you fill it, then it works.</p>\n'}]
['https://Stackoverflow.com/questions/74960', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13224/']