SAAJ API creates default namespace attribute xmlns=""
 Technote (troubleshooting)
 
Problem(Abstract)
Using the SAAJ API to add a child element to a parent element may cause the child to have a default namespace of xmlns="" if a namespace is not explicitly declared for the child.
 
Cause
For example:

Name childName1 = envelope.createName("ChildElement");
SOAPElement childElement1 = bodyElement.addChildElement(childName1);


will cause the following to be added to the XML:


<ChildElement1 xmlns=""/>


Here is a larger example. If you run the following code:



MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart mypart = message.getSOAPPart();
SOAPEnvelope envelope = mypart.getEnvelope();
SOAPBody body = envelope.getBody();
Name bodyName = envelope.createName("RootElement", "ns","http://mypackage");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
Name childName1 = envelope.createName("ChildElement1");

//Use addChildElement method with a "Name" parameter,
//which will create "<ChildElement1 xmlns=""/>"
SOAPElement childElement1 = bodyElement.addChildElement(childName1);

//Use addChildElement method with a "String" parameter,
//which will create "<ChildElement2 xmlns=""/>"
SOAPElement childElement2 = bodyElement.addChildElement("ChildElement2");

message.writeTo(System.out);




The following output will appear:



<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Header/>

<soapenv:Body>

<ns:RootElement xmlns:ns="http://mypackage">

<ChildElement1 xmlns=""/>

<ChildElement2 xmlns=""/>

</ns:RootElement>

</soapenv:Body>

</soapenv:Envelope>

 
Resolving the problem
This functionality is by design for interoperability purposes. The default namespace is defined so that there is no mistake when interpreting to what namespace an element belongs.

In the past, there were interoperability problems because the xmlns="" was not inserted. For that reason, they now are being inserted. The xmlns="" ensures that the element does not inherit the namespace of the parent.

The specifications are not clear about if a child element without an xmlns attribute should inherit the parent namespace or not. Many key providers for Web service engines chose to say that the lack of the xmlns attribute meant that there was no namespace.

 
 
Cross Reference information
Segment Product Component Platform Version Edition
Application Servers Runtimes for Java Technology Java SDK
 
 


Document Information


Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server > Web Services (for example: SOAP or UDDI or WSGW or WSIF)
Operating system(s): Windows
Software version: 5.1.1
Software edition:
Reference #: 1210637
IBM Group: Software Group
Modified date: Jul 28, 2006