<schema xmlns="http://www.w3.org/2001/XMLSchema">
<!-- schema content goes here -->
</schema>
The XSD namespace has changed quite a bit as the XSD specification itself has evolved. In the final specification, the XSD namespace is http://www.w3.org/2001/XMLSchema. If you come across an XSD schema that’s using a different namspace, you’ll know it’s written to an older (possibly draft) version of the XSD standard.
Within a schema, you define data types and declare elements. For example, assume you have this simple XML document:
<top>
<child1>text</child1>
<child2>text</child2>
</top>
The schema would first define a type that describes the contents of the <top> element. It would then declare and element of that type:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name=" topType">
<xsd:sequence>
<xsd:element name="child1" type="xsd:string"/>
<xsd:element name="child2" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name=" top" type=" topType"/>
</schema>
<xsd:complexType>
defines a data type that contains child
elements and/or attributes. The <top>
element contains child
element so it must be a complexType. Within this complex type, we specify that
there will be a sequence of child1 and child2 elements. <xsd:sequence>
means the elements must appear in that order, so if the XML document had <child2>
first then <child1>
that would violate the sequence and would
be a validation error. Using <xsd:element>
we declare each
of the <child1>
and <child2>
elements and
specify their data type as xsd:string. Note the use of the xsd: namespace prefix
to indicate that this is one of the XSD built-in data types.
Now that we have defined the data type that describes our <top>
element, we can declare the element itself using <xsd:element>
.
Note that we specify its type as topType which is the name we used for the complexType.
The basics of creating XSD schemas are: First define your data types then declare elements of those types. Lets take a look at some real-world examples.
Comments