0

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ya.
  • 1,671
  • 4
  • 27
  • 53

1 Answers1

2

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.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • Thanks, I just tried your suggestion, it looked really promising to me until... I realized that this would require namespace qualifiers on all elements, including incoming requests which is definitely not what I want. – Ya. Dec 13 '12 at 20:12
  • As far as the error I get when NOT specifying namespace in Value and ResponseString - looks like it's a known issue and the only workaround for them is to add the namespaces: http://stackoverflow.com/questions/4037125/namespace-err-an-attempt-is-made-to-create-or-change-an-object-in-a-way-which-i – Ya. Dec 13 '12 at 20:16
  • I may be missing something: from scanning that question I got the impression that another workaround existed: upgrading to a higher level of Xalan. If that's not an option for you, and you want to make *just* the two elements Value and ResponseString be ns-qualified, then add `form="qualified"` just to those two element declarations, instead of setting the element-form default on the xs:schema element. – C. M. Sperberg-McQueen Dec 13 '12 at 20:18
  • form"qualified" on those two elements does the job, thank you very much! Will probably try newer xalan too sometime. – Ya. Dec 13 '12 at 20:32