Ya.
Ya.

Reputation: 2657

XSD validation error - xml namespace issues

I'm running a simple web service and I have a problem constructing and validating a response. This issue relates to namespaces.

In my WSDL I have:

    <wsdl:definitions targetNamespace="http://me.com/mystuff">
      <wsdl:types><xs:schema targetNamespace="http://me.com/mystuf">
          ....

        <xs:complexType name="MyResponse">
        <xs:all>
           <xs:element name="Value" type="xs:boolean"/>
            <xs:element name="ResponseString" type="xs:string"/>
        </xs:all>
      </wsdl:types>

      //MyResponse is bound as a response for a soap operation
     </wsdl:definitions>

I first tried to construct the response as:

        String NAMESPACE_URI="http://me.com/mystuff";

    Namespace namespace = Namespace.getNamespace("xyz", NAMESPACE_URI);
    Element response = new Element(txType + "Response", namespace);

    Element value = new Element("Value");
    value.setText("true");
    response.addContent(value);

    Element responseString = new Element("ResponseString");
    response.addContent(responseString);
    responseString.setText("");

But I'd get:

        org.xml.sax.SAXException: Exception in startElement: NAMESPACE_ERR: An attempt  is made to create or change an object in a way which is incorrect with regard to namespaces.

org.jdom.JDOMException: Exception in startElement: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

So I added namespace declarations to the child elements in the response:

    Element value = new Element("Value", namespace);
    Element responseString = new Element("ResponseString", namespace);

But then I get:

    ERROR PayloadValidatingInterceptor:238 - XML validation error on response: cvc-complex-type.2.4.a: Invalid content was found starting with element 'xyz:Value'. One of '{Value, ResponseString}' is expected.

Any ideas on how to resolve this?

Upvotes: 0

Views: 1637

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25054

If you control the schema, add elementFormDefault="qualified" to the xs:schema element in your schema; that should make the namespace-qualified versions of Value and ResponseString you are now generating be valid.

If you don't control the schema, then the Value and ResponseString elements will need not to be namespace-qualified, which looks like what your original attempt was doing; I can't see what was going wrong there. If you can figure out which statement in particular was raising that error, it would help.

Upvotes: 2

Related Questions