Blue
Blue

Reputation: 73

cvc-complex-type.2.4.a: Invalid content was found starting with element 'xs:attribute'

I am very new to XML and XSD just trying to learn the basics. Can someone explain to me why this code gives an error.

<?xml version="1.0"?>

<xs:schema version="1.0"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       elementFormDefault="qualified">
<xs:element name="DreamHomes">
    <xs:complexType>
        <xs:sequence>
            <xs:element name ="Branch">
                <xs:complexType>
                    <xs:attribute name ="branchNo" type="xs:string" default="1"/>
                    <xs:sequence>
                        <xs:element name="Street">
                            <xs:simpleType>
                                <xs:restriction base="xs:string"/>
                            </xs:simpleType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

The error is to do with my attribute tag as when I remove it I have no errors. Why is this error here?

Upvotes: 1

Views: 1119

Answers (1)

kjhughes
kjhughes

Reputation: 111581

XSD

The xs:attribute declaration must appear after xs:sequence within xs:complexType:

<?xml version="1.0"?>
<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
  <xs:element name="DreamHomes">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Branch">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Street">
                <xs:simpleType>
                  <xs:restriction base="xs:string"/>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="branchNo" type="xs:string" default="1"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Sample XML

The following XML would be valid per the above XSD:

<DreamHomes>
  <Branch branchNo="2">
    <Street>123 Main</Street>
  </Branch>
</DreamHomes>

Upvotes: 1

Related Questions