'PHP SoapClient: set a namespace without prefix

I'm consuming a WSDL SOAP service using PHP's SoapClient. Here's the relevant bits of the WSDL: https://gist.github.com/jurchiks/03771c0b85683969bc48711e56693919

The generated XML contains the following namespace definitions:

<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="WebApplicationImport"
    ....>

And the elements from this ns1 namespace are all prefixed with ns1:elementName.

The service provider says that this is wrong and they want these elements without the prefix, even though the WSDL defines a requirement for it: xmlns:tns="WebApplicationImport" elementFormDefault="qualified".

One solution is to do the following before sending the XML: $xml = str_replace(':ns1', '', $xml);. This works, but is clearly a hack, and I'm not a fan of hacks.

Is there a way to tell SoapClient to handle a specific namespace as the default/do not prefix it? What I would like to achieve is <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns="WebApplicationImport"> before generating the rest of the envelope, which would, in theory, ensure that all elements from that namespace are also unprefixed. This would seem like the proper and valid solution to me.



Solution 1:[1]

WebApplicationImport is not a valid URI. So it is not a valid namespace. So the WSDL and you XML is actually invalid. some parser might tolerate namespaces like that but it should be something like urn:WebApplicationImport or http://example.com/ns/WebApplicationImport.

By removing the prefix you put the elements into the current default namespace and this should be the empty/non namespace in you case.

Interesting enough PHP DOM tolerates it in namespace definitions for prefixes but not for the default namespace:

$document = new DOMDocument();
$document->loadXML(
    '<foo xmlns="WebApplicationImport"/>'
);

Output:

Notice: DOMDocument::loadXML(): xmlns: URI WebApplicationImport is not absolute in Entity

So depending on the parser the service provider uses it could throw errors for WebApplicationImport as a namespace URI.

You should remove/fix the namespace in the WSDL. If you fix it then the service provider has to adapt his logic to use the new namespace.

Prefixes are only aliases for actual namespace URI. The following 3 examples can all be read as {urn:example}foo:

  • <foo xmlns="urn:example"/>
  • <ns1:foo xmlns:ns1="urn:example"/>
  • <f:foo xmlns:f="urn:example"/>

Solution 2:[2]

I had the same issue but in Java, the vendor was not accepting ns1 in the xmlns attribute. If you want to add xmlns attribute but without ns1 prefix, you can add the below code and check.

declare the SOAP header element:

soapHeader = new SOAPHeaderElement("", "HeaderElementName");

soapHeader.addAttribute("", "xmlns", "url");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jrswgtr