PB138/07 - Modern Markup
Languages and Their Applications
Lab 02 [2.3.2020]
XML Schema (XSD)
Bruno Rossi
Department of Computer Systems and Communications,
Lasaris (Lab of Software Architectures and Information Systems)
Masaryk University, Brno
2/12
●
XSD (XML Schema Definition): how to formally describe the
elements in an XML document. It is used to validate that a
specific XML conforms to the XSD definition
●
XSD: just another XML document
....
Quick Summary (1/2)
3/12
Quick Summary (2/2)
●
Different ways to create an XSD
●
See “Divide the Schema” and “Using Named Types”
https://www.w3schools.com/xml/schema_example.asp
https://www.w3schools.com/xml/schema_example.asp
1. Defining Data Types
→ text, numbers, restrictions
or
→ other elements, attributes, text
2. Assigning Data types to elements
→ type is a reference to the name used to
define the type
4/12
XML Schema (XSD)
●
We will use the same continent.xml example we used before
●
You can download the file named 02-xsd-ex.zip
●
In these tasks, you will have to modify the file continent.xsd
●
Run xmllint to check what's the results from schema validation
using file continent.xml
$> xmllint --noout --schema continent.xsd continent.xml
We have an empty XSD, will the validation
pass?
5/12
Exercise (1/6)
●
As you see, validation is not passing
●
Try to make the smallest change to the XSD that will make
validation pass
●
You can use
Do you need to declare the type here?
What is the type in this case?
Common types
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
6/12
Exercise (2/6)
●
Update the XSD to represent the structure of the XML file
Remember:
●
attributes can only be defined for complex types
●
Manage elements occurrence with minOccurs="0..n"
maxOccurs="0..n" (maxOccurs="unbounded")
●
Difference between , ,
●
Look at
https://www.w3.org/TR/xmlschema-0/ (2.1 the Purchase Order Schema)
7/12
Exercise (3/6)
●
Try to place a restriction on the maximum population of a city
(min:0, max:14200000)
●
Consider using
8/12
Exercise (4/6)
●
Try to place a restriction on the type of pollution
('low','medium','high)
●
Consider using
9/12
Exercise (5/6)
●
Try to place a restriction on initial letter of the city name: it must be
starting with capital letter (note that this should also allow cities
such as 'Los Angeles')
●
Consider using
10/12
Exercise (6/6)
●
Modify continent.xml to give both Tokio and Mumbai the same id:
...
Tokio
....
Mumbai
...
●
Check that the file still passes validation
●
Define either a key or unique constraint for the city id, the structure is:
●
Validation has not to pass. You should be getting something like Element 'city':
Duplicate key-sequence ['1'] in key identity-constraint 'id'.
11/12
For next time (optional)
Checkout the code at https://gitlab.fi.muni.cz/pb138/seminars/seminar-02
1) Use either the Java or Javascript implementation to validate the
schema created today – it should pass as with xmllint :)
2) in the src/ folder, complete schema.xsd for data.xml in similar way
we did today (you can also see a reminder of the syntax in
schema-ukazka.xsd)
12/12
References
Suggested material:
●
W3C XML Schema Definition Language (XSD) W3C Recommendation:
→ https://www.w3.org/TR/xmlschema-0/
●
XML Schema Tutorial on w3schools.com:
→ https://www.w3schools.com/xml/schema_intro.asp