I've made an XSD for a project, and this XSD should be able to catch errors related to missing structures on XML code but that's not the case. For example, when I try to validate an incorrect XML like:
<Course number="1">
<Subject idSub="s8" type="core">
<Name>PII</Name>
<Student>
<Name>John White</Name>
<ID>12345601A</ID>
<Grade>9.27</Grade>
</Student>
</Subject>
</Course>
where parent element Degree
and its subelements Name
and Scope
are missing, validator informs me that XML is valid when it should be wrong.
I've already tried to create a parent class as:
<xsd:element name="Degrees">
<xs:complexType>
<xs:sequence>
<xs:element ref="Degree" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xsd:element>
but it seems that does nothing.
This is an example of a valid XML:
<Degree location="London">
<Name>Industrial Engineering</Name>
<Scope>technology</Scope>
<Course number="1">
<Subject idSub="ts1" type="core">
<Name>Thermodynamics</Name>
<Student>
<Name>Michael Williams</Name>
<ID>89345601A</ID>
<Grade>7.37</Grade>
</Student>
</Subject>
</Course>
</Degree>
Here is of my full XSD code:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Degree">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element ref="Scope"/>
<xsd:element ref="Course" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="location" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="London"/>
<xsd:enumeration value="Oxford"/>
<xsd:enumeration value="Cambridge"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="Scope">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="humanities"/>
<xsd:enumeration value="science"/>
<xsd:enumeration value="technology"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Course">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Subject" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="number" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:pattern value="[1-4]{1}" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
<xsd:unique name="IDSubUnique">
<xsd:selector xpath="./Subject" />
<xsd:field xpath="@idSub" />
</xsd:unique>
</xsd:element>
<xsd:element name="Subject">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element ref="Student" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="type" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="core" />
<xsd:enumeration value="specialty" />
<xsd:enumeration value="optional" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="idSub" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="Student">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:choice>
<xsd:element ref="ID" minOccurs="0"/>
<xsd:element ref="Resident" minOccurs="0"/>
</xsd:choice>
<xsd:element ref="Grade"/>
<xsd:element ref="EAML" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="StudentAddress" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="ID">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{8}[A-Z]{1}"></xsd:pattern>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Resident">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[A-Z]{1}[0-9]{7}"></xsd:pattern>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Grade">
<xsd:simpleType>
<xsd:restriction base="xsd:decimal">
<xsd:fractionDigits value="2"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="EAML" type="xsd:anyURI"/>
</xsd:schema>