1

I have the following XSD:

<?xml version="1.0"?>
<xs:schema xmlns:item="http://www.example.org/ItemSchema"
           targetNamespace="http://www.example.org/ItemSchema"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="Item">
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-fA-F]"/>
            <xs:length value="1"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="ItemWithAttr">
        <xs:simpleContent>
            <xs:extension base="item:Item">
                <xs:attribute name="C" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:positiveInteger">
                            <xs:pattern value="[0-9]{4}"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:element name="A">
        <xs:complexType>
            <xs:all>
                <xs:element name="B" type="item:ItemWithAttr" minOccurs="0"/>
                <xs:element name="D" type="item:ItemWithAttr" minOccurs="0"/>
            </xs:all>
        </xs:complexType>
    </xs:element>

</xs:schema>

And the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<A xmlns="http://www.example.org/ItemSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.example.org/ItemSchema abcd.xsd">
    <B C="0231">a</B>
    <B C="3124">a</B>
    <B C="4114">b</B>
    <B C="0312">b</B>
    <B C="1543">d</B>
    <B C="2345">b</B>
    <D C="1111">d</D>
    <D C="4321">b</D>
</A>

I have tried to validate XML against XSD here, and have gotten this error:

Not valid. Error - Line 5, 17: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 17; cvc-complex-type.2.4.a: Invalid content was found starting with element 'B'. One of '{B, D}' is expected.

What's wrong with it? Everything seems to be correct.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
SanchelliosProg
  • 2,091
  • 3
  • 20
  • 32

1 Answers1

1

Make the following corrections to your XSD (#3 is the hardest to spot):

  1. Add maxOccurs="unbounded" to the declarations of B and D because you have multiple B and D elements in your XML, and the default for @maxOccurs is 1.
  2. Change xs:all to xs:sequence because xs:all children cannot be unbounded in XSD 1.0 (and because you have a sequence in your XML anyway).
  3. Add elementFormDefault="qualified" because by default elements with a local declaration are unqualified in the XML document.

Altogether, this XSD will validate your XML:

<?xml version="1.0"?>
<xs:schema xmlns:item="http://www.example.org/ItemSchema"
           targetNamespace="http://www.example.org/ItemSchema"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">    
  <xs:simpleType name="Item">
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-fA-F]"/>
      <xs:length value="1"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="ItemWithAttr">
    <xs:simpleContent>
      <xs:extension base="item:Item">
        <xs:attribute name="C" use="required">
          <xs:simpleType>
            <xs:restriction base="xs:positiveInteger">
              <xs:pattern value="[0-9]{4}"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>    
  <xs:element name="A">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="B" type="item:ItemWithAttr" 
                    minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="D" type="item:ItemWithAttr" 
                    minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240