Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Weeks 2-3 Schema

Internet Technologies and Web


Services
Aims of the lecture
• At the end of today’s lecture students should
be able to
– Understand what is XSD and what it is used for.
– Write XSD to validate an existing XML

2
XML Schema (XSD)
• XML Schema also called XSD (XML Schema
Definition) is used to validate XML documents
• Question: The XML is already being validated for
well-formedness, so why do we need XSD?
• XSD checks the following:
• Structure of an XML file.
• Type of data (datatype) of the XML elements.
• Legal values (restriction) for the XML elements.
• Validating an XML against an XSD requires a
special parser; the schema validator.
XSD document structure
Sample XML
<?xml version="1.0" encoding="utf-8"?>
<Person>John Smith</Person>
XSD to validate the above XML
How to link the XSD with the XML?
• The XML file and the XSD files should be linked
just like an HTML file is linked to a CSS file.
• The code to link the 2 is placed in the XML file
in the root element as shown below.
<Person xsi:noNamespaceSchemaLocation= " Person.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance">
Complex Types
• If the above XML file is modified as shown below
with Person now being a complex type.
<?xml version="1.0" encoding="utf-8"?>
<Person
xsi:noNamespaceSchemaLocation=“Person.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance">
<Name>John</Name>
<Surname>Smith</Surname>
</Person>
Amended XSD for new XML
Further modifying of the xml file
• Let us say that the XML file is further modified and an
element Lecturer is added after Person (to show the
post of the person) as shown below
<?xml version="1.0" encoding="utf-8"?>
<Person xsi:noNamespaceSchemaLocation=“Person.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Lecturer>
<Name>John</Name>
<Surname>Smith</Surname>
</Lecturer>
</Person>
Classwork 1
• Write the Amended XSD file to validate the
above XML file.
Classwork 2 – write the XML file that is being
validated by classwork2.xsd given below
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Modules">
<xs:complexType> <xs:all>
<xs:element name="Module">
<xs:complexType> <xs:sequence>
<xs:element name="Code" type="xs:string"/>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Level" type="xs:positiveInteger"/>
</xs:sequence> </xs:complexType>
</xs:element>
</xs:all> </xs:complexType>
</xs:element>
</xs:schema>
Attributes
• XML files very often contain attributes.
• The person.xml file is now amended and the
following attribute is added as follows:
<!– Missing Code -->
<Lecturer title="Mr">
<Name>John</Name>
<Surname>Smith</Surname>
</Lecturer> </Person>
Further Amended XSD
• the following is added before the closing tag
of the ComplexType for the Lecturer Element.
• <xs:attribute name="title" type="xs:string"/>
Restriction of values – more to follow
next week
• Now the allowable values of the title attribute are to be restricted
to Mr, Miss, Ms and Mrs.
• Note: Restriction is done on data (simpleType in this case).
• Change
<xs:attribute name="title" type="xs:string"/>
• to
<xs:attribute name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="Mr|Miss|Ms|Mrs"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
Regular Expressions
• You now want to restrict the Name and Surname
to a maximum of 20 characters only (excluding
space).
• The code above should be modified keeping in
mind the following:
• The types of both elements need to be changed
• Restriction should be used
• A pattern containing the regular expression
should be used
The Name Element
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z]{1,20}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The Surname Element
<xs:element name="Surname">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z]{1,20}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
Code duplication & Global types
• When examining the code for Name and
Surname elements, we notice there is code
duplication.
• This can be handled by making use of global
types.
• Global Types are always defined as a child of
Schema element (usually written just after
<xs:schema>).
• A simpleType ‘CharsOnly’ will now be defined.
• It is defined at the top and the type of both Name
and Surname are set accordingly.
simpleType
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="CharsOnly">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z]{1,20}"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="Person">
...
<xs:sequence>
<xs:element name="Name" type="CharsOnly"/>
<xs:element name="Surname" type="CharsOnly"/>
</xs:sequence>
<xs:attribute name="title">
More global types
• OfficeAddress is included in the XML file as shown below.
• Create a Global complexType named OfficeAddressType and set the
OfficeAddress element to that type.
<?xml version="1.0" encoding="utf-8"?>
<Person xsi:noNamespaceSchemaLocation="XSDEx2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Lecturer title="Mr">
<Name>John</Name>
<Surname>Smith</Surname>
<OfficeAddress>
<Room>1.4</Room>
<Department>Computer Science</Department>
<Faculty>Engineering</Faculty>
</OfficeAddress>
</Lecturer>
</Person>
complexType
<xs:complexType name="OfficeAddressType">
<xs:sequence>
<xs:element name="Room" type="xs:string"/>
<xs:element name="Department" type="xs:string"/>
<xs:element name="Faculty" type="xs:string"/>
</xs:sequence>
</xs:complexType>
...
<xs:sequence>
<xs:element name="Name" type="CharsOnly"/>
<xs:element name="Surname" type="CharsOnly"/>
<xs:element name="OfficeAddress" type="OfficeAddressType"/>
</xs:sequence>
MaxOccurs and MinOccurs
• In the above example there is only one child element, now more
child elements will be added.
<Lecturer title="Mrs">
<Name>Janet</Name>
<Surname>Higgins</Surname>
<ContactDetails>
<Room>3</Room>
<Department>Computer Science</Department>
<Faculty>Engineering</Faculty>
<Extension>7752</Extension>
<Email>j.higgins@uom.ac.mu</Email>
</ContactDetails>
</Lecturer>
MaxOccurs and MinOccurs (2)
• The xsd should now be amended to cater for this new information.
• The XML file now holds more than 1 lecturer
– Therefore the occurrence constraints maxOccurs/minOccurs
should be used.
• Lecturer can have either OfficeAddress or ContactDetails
– The schema element <xs:choice> should be used
• Office Address contains 3 elements namely Room, Department and
Faculty and is already being validated by the OfficeAddressType
complexType. ContactDetails contains Extension and Email
elements in addition to all of the elements of OfficeAddress.
– A new type ContactDetails should be created by extending the
OfficeAddressType .
MaxOccurs and MinOccurs (3)
• The number of time an element appears in an
XML document can be controlled by the
Occurrence Constraints attributes maxOccurs or
minOccurs
• The default for both maxOccurs and minOccurs is
‘1’.
• The lecturer element includes the attribute
maxOccurs to indicate that the XML file may
contain more than one occurrence of same.
minOccurs=‘1’ is assumed.
<xs:choice>
• <xs:choice> functions similar to a java switch
statement.
• The XML will validate as long as the Lecturer
element contains an OfficeAddress or a
ContactDetails element, but NOT both.
<xs:choice> (2)
<xs:element name="Lecturer" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="CharsOnly"/>
<xs:element name="Surname" type="CharsOnly"/>
<xs:choice>
<xs:element name="OfficeAddress"
type="OfficeAddressType"/>
<xs:element name="ContactDetails"
type="ContactDetailsType"/>
</xs:choice>
</xs:sequence>
Extending Global Types
• Global types can be extended to
accommodate new types. This is similar to
inheritance in java.
• ContactDetails differs from OfficeAddress such
that it contains two more elements. Since a
global type OfficeAddressType already exists,
this can be extended to include the other two
elements.
• Base refers to the type being extended.
OfficeAddressType
• <xs:complexType name="OfficeAddressType">
• <xs:sequence>
• <xs:element name="Room" type="xs:string"/>
• <xs:element name="Department"
type="xs:string"/>
• <xs:element name="Faculty" type="xs:string"/>
• </xs:sequence>
• </xs:complexType>
The content model (complexContent)
• Since ContactDetailsType is being extended
and contains elements, the complexContent
model is used.
The content model (complexContent)
<xs:complexType name="ContactDetailsType">
<xs:complexContent>
<xs:extension base="OfficeAddressType">
...
</xs:extension>
</xs:complexContent>
</xs:complexType>
ContactDetailsType
<xs:complexType name="ContactDetailsType">
<xs:complexContent>
<xs:extension base="OfficeAddressType">
<xs:sequence>
<xs:element name="Extension"
type="xs:positiveInteger"/>
<xs:element name="Email" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
The content model (simpleContent)
• Assume that the XML is modified such that the
Room element includes an attribute building as
shown below
• The element Room is a complex type since it has
an attribute building.
• It also contains the character data 7.1
• Create a new global type RoomType to validate
the above.
• When using the content model, attributes are
placed inside the extension/restriction
The XSD
<xs:complexType name="RoomType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="building"
type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
Classwork
Consider the XML shown below.
<?xml version="1.0" ?>
<Cars>
<Car Reconditioned="yes">
<Brand>Toyota</Brand>
<Model>Vitz</Model>
<Number>8099 ZN 00</Number>
<img src="car1.jpg"></img>
</Car>
<Car Reconditioned="no"> This model of car is a best-seller.
<Brand>Nissan</Brand>
<Model>March</Model>
<Number>99 ZX 09</Number>
<img src="car2.jpg"></img>
</Car>
<Car Reconditioned="yes">
<Brand>Kia</Brand>
<Model>Picanto</Model>
<Number>1238 ZX 10</Number>
<img src="car3.jpg"></img>
</Car>
</Cars>
Classwork continued
• Is the above xml document well-formed? Explain your answer.

• List (with justifications) the complex elements in the above code.

• Write the missing code in the file cars.xsd below.


<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="cars">
<xsd:complexType>
<xsd:sequence>
//missing code here
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
References
• http://www.w3schools.com
• CSE2041 Lecture notes
• CSE2003Y Lecture notes

You might also like