Skip to content
UoL CS Notes

XML Schema

COMP207 Lectures

This is the second way of defining the format of an XML file.

Comparison to DTD

XML schema should be used if you want more precision on the schema than offered by DTD.

Positives:

  • Defined in XML.
  • Give bounds on number of occurrences.
  • Can do unordered sets.
  • Can do more precise references.
  • More complex simple data types.
    • A number > 1915.

Negative:

  • The syntax is heavy.

Syntax

To get an XML schema version of the following DTD:

<!ELEMENT lecturers (lecturer+)>
<!ELEMENT lecturer (name, phone?, email?, teaches*)>
<!ELEMENT teaches (code, title)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT code (#PCDATA)>
<!ELEMENT title (#PCDATA)>

we could write something like the following:

<? xml version="1.0" encoding="utf-8” ?>
<!-- anything prefixed with xs: is an object -->
<!-- also means we are defining a "University" schema -->
<xs:schema xmlns: xs= “http://www.w3.org/2001/XMLSchema” targetNamespace=“University”>
	
	<xs:element name = “lecturers”>
		<xs:complexType>
			<xs:sequence>
				<xs:element name = “lecturer” type = “lecturerType” minOccurs = “1” maxOccurs = “unbounded”/>
			</xs:sequence>
		</xs:complexType>

		<xs:complexType name = “lecturerType”>
			<xs:sequence>
				<xs:element name=“name” type = “xs:string”/>
				<xs:element name=“phone” type = “xs:string” minOccurs = “0”/>
				<xs:element name=“email” type = “xs:string” minOccurs = “0”/>
				<xs:element name=“teaches” type = “theachesType” minOccurs = “0” maxOccurs = “unbounded”/>
			</xs:sequence>
		</xs:complexType>

		<xs:complexType name = “theachesType”>
			<xs:sequence>
				<xs:element name=“code” type = “xs:string”/>
				<xs:element name=“title” type = “xs:string”/>
			</xs:sequence>
		</xs:complexType>

		<xs:simpleType name="phoneType"> 
			<xs:restriction base="xs:string">
				<xs:pattern value=”07[0-9]{3} [0-9]{6}"/> 
			</xs:restriction> 
		</xs:simpleType>

		...
	</xs:element>
</xs:schema>

Convert Database to XML

There is an example of converting a table to an XML document at the end of the lecture slides