Kp88
Kp88

Reputation: 15

How to remove namespace prefix from XML response using XSD?

I have generated Java classes from an XSD, and in my XML responses I am getting a ns2: prefix which I do not need.

For example, I am getting this response:

<ns2:location xmlns:ns2="http://www.example.com/">
  <ns2:response/>
</ns2:location>

But the response I am expecting is as follows:

<location>
  <response/>
</location>

Upvotes: 0

Views: 8275

Answers (1)

kjhughes
kjhughes

Reputation: 111726

Your example shows not just a namespace prefix (ns2) declaration but also an actual namespace (http://www.example.com/ for the location and response elements) as not being desired.

If you have full control over the XSD, you can remove the

targetNamespace="http://www.example.com/"

attribute from the xs:schema element in the XSD. Elements of XML documents conforming to this XSD will then no longer be in any namespace, and your generated Java classes will reflect this new setting.

However, be aware that deleting (or changing) targetNamespace effectively changes the names of the components defined in the XSD. Think twice about making such a change if the XSD is defined by another party or otherwise already in use by others.

Upvotes: 3

Related Questions