Question

I'm new to XML and in trouble validating XML Schema with an instance!

My XSD:

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="result">
    <xs:complexType>
      <xs:all>
        <xs:element name="response">
          <xs:complexType>
            <xs:all>
              <xs:element minOccurs="0" name="status" type="xs:string" />
              <xs:element minOccurs="0" name="messages" nillable="true">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="item">
                      <xs:complexType>
                        <xs:all>
                          <xs:element maxOccurs="1" minOccurs="0" name="date" type="xs:string"/>
                          <xs:element maxOccurs="1" minOccurs="0" name="department" type="xs:string"/>
                          <xs:element maxOccurs="1" minOccurs="0" name="msg-from" type="xs:string"/>
                          <xs:element maxOccurs="1" minOccurs="0" name="reply1" type="xs:string"/>
                          <xs:element maxOccurs="1" minOccurs="0" name="reply2" type="xs:string"/>
                        </xs:all>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:all>
          </xs:complexType>
        </xs:element>
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

My XML Instance:

<result xmlns:xsi="http://www.w3.org/2001/XMLSchema">
    <response>
        <status>No new messages</status>
        <messages xsi:nil="true"/>
    </response>
</result>

When I'm trying to validate the instance against the XSD using tools like this, I'm getting following error:

cvc-complex-type.3.2.2: Attribute 'xsi:nil' is not allowed to appear in element 'messages'.

Which I don't understand because I've already put nillable="true" for messages element!

Your help will be highly appreciated! Thanks.

Was it helpful?

Solution

I figured out that putting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" in the XML instance validates the XML instance!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top