Search code examples
javaxmlxsdxsd-validationxml-validation

XSD element namespace validation failure


I am working in Java application where I need to show XML output as result of one GET REST URL execution.

I am getting error as:

cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.example.org/xmlns/sapsaasconnection/SAPAriba/CATALOGCONTENT+facets_GET_Responses+286d4d67-dbb4-4187-ac9e-a9dd069c3035":Output}'. One of '{Output}' is expected.

My XSD is:

<schema targetNamespace="http://www.example.org/xmlns/sapsaasconnection/SAPAriba/CATALOGCONTENT+facets_GET_Responses+286d4d67-dbb4-4187-ac9e-a9dd069c3035"
        xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:tns="http://www.example.org/xmlns/sapsaasconnection/SAPAriba/CATALOGCONTENT+facets_GET_Responses+286d4d67-dbb4-4187-ac9e-a9dd069c3035"
        >
  <complexType name="facets_GET_Responses">
    <sequence>
      <element maxOccurs="1" minOccurs="0" name="Output" type="tns:FacetResponse"/>
    </sequence>
  </complexType>
  <element name="facets_GET_Responses" type="tns:facets_GET_Responses"/>
  <complexType name="MapStringToObject">
    <all>
      <element maxOccurs="1" minOccurs="0" name="count" type="integer"/>
      <element maxOccurs="1" minOccurs="0"
               name="ClassificationCode" type="string"/>
    </all>
  </complexType>
  <element name="MapStringToObject" type="tns:MapStringToObject"/>
  <complexType name="FacetResponse">
    <all>
      <element maxOccurs="1" minOccurs="0" name="contents" type="tns:contents"/>
      <element maxOccurs="1" minOccurs="0" name="offset" type="integer"/>
      <element maxOccurs="1" minOccurs="0" name="limit" type="integer"/>
      <element maxOccurs="1" minOccurs="0"
               name="totalElements" type="integer"/>
      <element maxOccurs="1" minOccurs="0"
               name="totalPages" type="integer"/>
      <element maxOccurs="1" minOccurs="0"
               name="firstPage" type="boolean"/>
      <element maxOccurs="1" minOccurs="0" name="lastPage" type="boolean"/>
    </all>
  </complexType>
  <element name="FacetResponse" type="tns:FacetResponse"/>
  <complexType name="contents">
    <sequence>
      <element maxOccurs="unbounded" minOccurs="0"
               name="items" type="tns:MapStringToObject"/>
    </sequence>
  </complexType>
</schema>

My XML output is:

<ns0:facets_GET_Responses xmlns:ns0="http://www.example.org/xmlns/sapsaasconnection/SAPAriba/CATALOGCONTENT+facets_GET_Responses+286d4d67-dbb4-4187-ac9e-a9dd069c3035">
    <ns0:Output>
        <ns0:contents>
            <ns0:items>
                <ns0:count>42</ns0:count>
                <ns0:ClassificationCode>UNSPSC:1000030</ns0:ClassificationCode>
            </ns0:items>
        </ns0:contents>
        <ns0:limit>25</ns0:limit>
        <ns0:offset>0</ns0:offset>
        <ns0:totalElements>25</ns0:totalElements>
        <ns0:totalPages>1</ns0:totalPages>
        <ns0:firstPage>true</ns0:firstPage>
        <ns0:lastPage>true</ns0:lastPage>
    </ns0:Output>
</ns0:facets_GET_Responses>

I tried to analyze it in my way but didn't find what's wrong here. Can anybody help here as what is expected in XSD schema?


Solution

  • If you wish to change your XSD, add elementFormDefault="unqualified" to the schema element.

    If you wish to change your XML, remove the ns0 namespace prefix from all element tags except ns0:facets_GET_Responses.

    See What does elementFormDefault do in XSD? for detailed explanations regarding elementFormDefault.