05 - XML - Schemas
05 - XML - Schemas
0> <course startdate=February 06, 2006> <title> eXtensible Markup Language </title> <lecturer>Phan Vo Minh Thang</lecturer> </course>
30 Second Intro
On the next 3 slides is a very quick, high-level introduction to XML Schemas. The purpose is to give you the "big picture" before we jump into all the nitty-gritty details of creating XML Schemas.
Example
<location> <latitude>32.904237</latitude> <longitude>73.620290</longitude> <uncertainty units="meters">2</uncertainty> </location>
Is this data valid? To be valid, it must meet these constraints (data business rules): 1. The location must be comprised of a latitude, followed by a longitude, followed by an indication of the uncertainty of the lat/lon measurements. 2. The latitude must be a decimal with a value between -90 to +90 3. The longitude must be a decimal with a value between -180 to +180 4. For both latitude and longitude the number of digits to the right of the decimal point must be exactly six digits. 5. The value of uncertainty must be a non-negative integer 6. The uncertainty units must be either meters or feet. We can express all these data constraints using XML Schemas
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
XML
Data is ok!
Declare a location element. Require that its content be latitude, longitude, and uncertainty. Declare a latitude element. Require that its value be between -90 and +90. Declare a longitude element. Require that its value be between -180 and +180. Declare a uncertainty element with a units attribute. Require that the element's value be between 0 and 10. Require that the attribute's value be either feet or meters.
XML Schema
eXtensible Markup Language
An XML Schema specifies that the XML vocabulary that is being created shall be in a "namespace"
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
http://www.example.org/target Namespace
<uncertainty>
BookStore.dtd
<!ELEMENT BookStore (Book+)> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)>
ELEMENT ATTLIST #PCDATA ID NMTOKEN ENTITY CDATA Title Publisher ISBN Date Book BookStore Author
This is the vocabulary that DTDs provide to define your new vocabulary
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
complexType element sequence schema string integer boolean Title Publisher ISBN Date Book BookStore Author
This is the vocabulary that XML Schemas provide to define your new vocabulary
One difference between XML Schemas and DTDs is that the XML Schema vocabulary is associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). With DTDs neither set of vocabulary is associated with a name (namespace) [because DTDs pre-dated namespaces]. eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:schema> BookStore.xsd (see example01)
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <!ELEMENT BookStore (Book+)> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <!ELEMENT Book (Title, Author, Date, <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> ISBN, Publisher)> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <!ELEMENT Title (#PCDATA)> <xsd:element name="Author" type="xsd:string"/> <!ELEMENT Author (#PCDATA)> <xsd:element name="Date" type="xsd:string"/> <!ELEMENT Date (#PCDATA)> <xsd:element name="ISBN" type="xsd:string"/> <!ELEMENT ISBN (#PCDATA)> <xsd:element name="Publisher" type="xsd:string"/> <!ELEMENT Publisher (#PCDATA)> </xsd:schema> eXtensible Markup Language
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" All XML Schemas have targetNamespace="http://www.books.org" xmlns="http://www.books.org" "schema" as the root elementFormDefault="qualified"> element. <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> eXtensible Markup Language </xsd:schema>
Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" The elements and elementFormDefault="qualified"> datatypes that <xsd:element name="BookStore"> are used to construct <xsd:complexType> schemas <xsd:sequence> - schema <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> - element </xsd:sequence> - complexType </xsd:complexType> - sequence </xsd:element> - string <xsd:element name="Book"> come from the <xsd:complexType> http:///XMLSchema <xsd:sequence> namespace <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> eXtensible Markup Language </xsd:schema>
Lecturer: Phan Vo Minh Thang MSc.
XMLSchema Namespace
http://www.w3.org/2001/XMLSchema
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> Indicates that the <xsd:element name="BookStore"> elements defined <xsd:complexType> by this schema <xsd:sequence> - BookStore <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> - Book </xsd:sequence> - Title </xsd:complexType> - Author </xsd:element> - Date <xsd:element name="Book"> - ISBN <xsd:complexType> - Publisher <xsd:sequence> are to go in the <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> http://www.books.org <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> namespace <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> eXtensible Markup Language </xsd:schema>
Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> The default namespace is <xsd:sequence> http://www.books.org <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> which is the </xsd:sequence> targetNamespace! </xsd:complexType> </xsd:element> <xsd:element name="Book"> This is referencing a <xsd:complexType> Book element declaration. <xsd:sequence> The Book in what <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> namespace? Since there <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> is no namespace qualifier <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> it is referencing the Book <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> element in the default <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> namespace, which is the </xsd:sequence> targetNamespace! Thus, </xsd:complexType> this is a reference to the </xsd:element> Book element declaration <xsd:element name="Title" type="xsd:string"/> in this schema. <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> eXtensible Markup Language </xsd:schema>
Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> This is a directive to any <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> instance documents which </xsd:sequence> conform to this schema: </xsd:complexType> Any elements used by the </xsd:element> instance document which <xsd:element name="Book"> were declared in this <xsd:complexType> schema must be namespace <xsd:sequence> qualified. <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> eXtensible Markup Language </xsd:schema>
Lecturer: Phan Vo Minh Thang MSc.
1. First, using a default namespace declaration, tell the schema-validator that all of the elements used in this instance document come from the http://www.books.org namespace. 2. Second, with schemaLocation tell the schema-validator that the http://www.books.org namespace is defined by BookStore.xsd (i.e., schemaLocation contains a pair of values). 3. Third, tell the schema-validator that the schemaLocation attribute we are using is the one in eXtensible Markup Language the XMLSchema-instance namespace.
XMLSchema-instance Namespace
http://www.w3.org/2001/XMLSchema-instance
schemaLocation="http://www.books.org BookStore.xsd"
targetNamespace="http://www.books.org"
A schema defines a new vocabulary. Instance documents use that new vocabulary.
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
BookStore.xml
BookStore.xsd
XMLSchema.xsd (schema-for-schemas)
Validate that the xml document conforms to the rules described in BookStore.xsd
Validate that BookStore.xsd is a valid schema document, i.e., it conforms to the rules described in the schema-for-schemas
<xsd:element ref="Title"/>
Do Lab1
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
<?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <element name="BookStore"> <complexType> <sequence> <element ref="bk:Book" maxOccurs="unbounded"/> </sequence> </complexType> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> <element ref="bk:Author"/> <element ref="bk:Date"/> <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> </sequence> </complexType> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema>
Note that http:///XMLSchema is the default namespace. Consequently, there are no namespace qualifiers on - schema - element - complexType - sequence - string
(see example02)
<?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <element name="BookStore"> <complexType> <sequence> <element ref="bk:Book" minOccurs="1" maxOccurs="unbounded"/> Here we are </sequence> referencing a </complexType> </element> Book element. <element name="Book"> Where is that <complexType> Book element <sequence> defined? In <element ref="bk:Title"/> what namespace? <element ref="bk:Author"/> The bk: prefix <element ref="bk:Date"/> indicates what <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> namespace this </sequence> element is in. bk: </complexType> has been set to </element> be the same as the <element name="Title" type="string"/> targetNamespace. <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
http://www.w3.org/2001/XMLSchema
http://www.books.org (targetNamespace)
bk
Do Lab1.1
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Note that we have moved all the element declarations inline, and we are no longer ref'ing to the element declarations. This results in a much more compact schema!
(see example03)
This way of designing the schema - by inlining everything - is called the Russian Doll design. eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
(see example03)
Named Types
The following slide shows an alternate (equivalent) schema which uses a named complexType.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="BookPublication"> Named type <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
The advantage of splitting out Book's element declarations and wrapping them in a named type is that now this type can be reused by other elements.
(see example04)
Please note that: <xsd:element name="A" type="foo"/> <xsd:complexType name="foo"> <xsd:sequence> <xsd:element name="B" /> <xsd:element name="C" /> </xsd:sequence> </xsd:complexType> is equivalent to: <xsd:element name="A"> <xsd:complexType> Element A has the complexType definition <xsd:sequence> inlined in the element <xsd:element name="B" /> declaration. <xsd:element name="C" /> </xsd:sequence> </xsd:complexType> </xsd:element> eXtensible Markup Language
Element A references the complexType foo.
An element declaration can have a type attribute, or a complexType child element, but it cannot have both a type attribute and a complexType child element.
A simple type A nonnegative A nonnegative (e.g., xsd:string) integer integer or "unbounded" or the name of Note: minOccurs and maxOccurs can only a complexType (e.g., BookPublication) be used in nested (local) element declarations.
<xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:complexType> </xsd:complexType> </xsd:element> eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
Problem
Defining the Date element to be of type string is unsatisfactory (it allows any string value to be input as the content of the Date element, including non-date strings).
We would like to constrain the allowable content that Date can have. Modify the BookStore schema to restrict the content of the Date element to just date values (actually, year values. See next two slides).
Similarly, constrain the content of the ISBN element to content of this form: dddddd-ddd-d or d-ddd-ddddd-d or d-dd-dddddd-d, where 'd' stands for 'digit'
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}"/> <xsd:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/> <xsd:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> <xsd:element name="ISBN" type="ISBNType"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Declaring Date to be of type gYear, and ISBN to be of type ISBNType (defined above)
(see example05)
<xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}"/> <xsd:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/> <xsd:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/> </xsd:restriction> </xsd:simpleType>
"I hereby declare a new type called ISBNType. It is a restricted form of the string type. Elements declared of this type must conform to one of the following patterns: - First Pattern: 1 digit followed by a dash followed by 5 digits followed by another dash followed by 3 digits followed by another dash followed by 1 more digit, or - Second Pattern: 1 digit followed by a dash followed by 3 digits followed by another dash followed by 5 digits followed by another dash followed by 1 more digit, or - Third Pattern: 1 digit followed by a dash followed by 2 digits followed by another dash followed by 6 digits followed by another dash followed by 1 more digit." These patterns are specified using Regular Expressions. In a few slides eXtensible Markup Language we will see more of the Regular Expression syntax.
Equivalent Expressions
<xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}"/> <xsd:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/> <xsd:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"/> </xsd:restriction> </xsd:simpleType>
eXtensible Markup Language The vertical bar means "or"Lecturer: Phan Vo Minh Thang MSc.
<xsd:complexType> or <xsd:simpleType>?
When do you use the complexType element and when do you use the simpleType element?
Use the complexType element when you want to define child elements and/or attributes of an element Use the simpleType element when you want to create a new type that is a refinement of a built-in type (string, date, gYear, etc)
Built-in Datatypes
Primitive Datatypes string boolean decimal float double duration dateTime time date gYearMonth gYear gMonthDay
P1Y2M3DT10H30M12.3S
format: CCYY-MM-DDThh:mm:ss format: hh:mm:ss.sss format: CCYY-MM-DD format: CCYY-MM format: CCYY format: --MM-DD
Note: 'T' is the date/time separator INF = infinity eXtensible Markup Language NAN = not-a-number
Atomic, built-in
format: ---DD (note the 3 dashes) format: --MM a hex string a base64 string http://www.xfront.com a namespace qualified name a NOTATION from the XML spec
Subtype of primitive datatype A string without tabs, line feeds, or carriage returns String w/o tabs, l/f, leading/trailing spaces, consecutive spaces
any valid xml:lang value, e.g., EN, FR, ...
must be used only with attributes must be used only with attributes must be used only with attributes must be used only with attributes part (no namespace qualifier) must be used only with attributes must be used only with attributes must be used only with attributes 456 negative infinity to 0
negative infinity to -1
-9223372036854775808 to 9223372036854775807
-2147483648 to 2147483647 -32768 to 32767 -127 to 128 0 to infinity 0 to 18446744073709551615 0 to 4294967295 0 to 65535
0 to 255 1 to infinity
Note: the following types can only be used with attributes (which we will discuss later): ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.
Do Lab 3
1. This creates a new datatype called 'TelephoneNumber'. 2. Elements of this type can hold string values, 3. But the string length must be exactly 8 characters long and 4. The string must follow the pattern: ddd-dddd, where 'd' represents a 'digit'. (Obviously, in this example the regular expression makes the length facet redundant.)
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
Another Example
<xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction> </xsd:simpleType>
This creates a new type called shape. An element declared to be of this type must have either the value circle, or triangle, or square.
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
Example
<xsd:simpleType name= "EarthSurfaceElevation"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction> </xsd:simpleType> This creates a new datatype called 'EarthSurfaceElevation'. Elements declared to be of this type can hold an integer. However, the integer is restricted to have a value between -1290 and 29035, inclusive.
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<xsd:simpleType name= "name"> <xsd:restriction base= "xsd:source"> <xsd:facet value= "value"/> <xsd:facet value= "value"/> </xsd:restriction> </xsd:simpleType>
Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive ... Sources: - string - boolean - number - float - double - duration - dateTime - time ... eXtensible Markup Language
<xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction> </xsd:simpleType>
An element declared to be of type shape must be a string with a value of either circle, or triangle, or square.
Patterns, enumerations => "or" them together All other facets => "and" them together
Thus far we have created a simpleType using one of the built-in datatypes as our base type. However, we can create a simpleType that uses another simpleType as the base. See next slide.
<xsd:simpleType name= "EarthSurfaceElevation"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name= "BostonAreaSurfaceElevation"> <xsd:restriction base="EarthSurfaceElevation"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> </xsd:restriction> </xsd:simpleType> This simpleType uses EarthSurfaceElevation as its base type.
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<xsd:simpleType name= "ClassSize"> <xsd:restriction base="xsd:nonNegativeInteger"> <xsd:minInclusive value="10" fixed="true"/> <xsd:maxInclusive value="60"/> </xsd:restriction> </xsd:simpleType>
simpleTypes which derive from this simpleType may not change this facet.
<xsd:simpleType name= "ClassSize"> <xsd:restriction base="xsd:nonNegativeInteger"> <xsd:minInclusive value="10" fixed="true"/> <xsd:maxInclusive value="60"/> </xsd:restriction> </xsd:simpleType>
<xsd:simpleType name= "BostonIEEEClassSize"> <xsd:restriction base="ClassSize"> <xsd:minInclusive value="15"/> <xsd:maxInclusive value="60"/> </xsd:restriction> </xsd:simpleType>
<elevation>5240</elevation> Here's one way of declaring the elevation element: <xsd:simpleType name="EarthSurfaceElevation"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="elevation" type="EarthSurfaceElevation"/>
eXtensible Markup Language
Here's an alternative method for declaring elevation: <xsd:element name="elevation"> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction> </xsd:simpleType> </xsd:element>
The simpleType definition is defined inline, it is an anonymous simpleType definition. The disadvantage of this approach is that this simpleType may not be reused by other elements.
<xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:complexType> </xsd:complexType> </xsd:element> <xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:simpleType> <xsd:restriction base="type"> </xsd:restriction> </xsd:simpleType> </xsd:element> eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
Annotating Schemas
The <annotation> element is used for documenting the schema, both for humans and for programs. Use <documentation> for providing a comment to humans Use <appinfo> for providing a comment to programs
The content is any well-formed XML
<xsd:annotation> <xsd:documentation> The following constraint is not expressible with XML Schema: The value of element A should be greater than the value of element B. So, we need to use a separate tool (e.g., Schematron) to check this constraint. We will express this constraint in the appinfo section (below). </xsd:documentation> <xsd:appinfo> <assert test="A > B">A should be greater than B</assert> </xsd:appinfo> <xsd:/annotation>
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Suppose that you want to annotate, say, the Date element declaration. What do we do? See next page ...
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"> <xsd:annotation> <xsd:documentation>This is how to annotate the Date element!</xsd:documentation> </xsd:annotation> </xsd:element> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Lecturer: Phan Vo Minh Inline the annotation within the Date element declaration. Thang MSc.
In the previous example we showed <xsd:documentation> with no attributes. Actually, it can have two attributes:
source: this attribute contains a URL to a file which contains supplemental information xml:lang: this attribute specifies the language that the documentation was written in
<xsd:appinfo source="http://www.thangpvm.com/Assertions.xml"/>
Up for a Breath
Wow! We have really been into the depths of XML Schemas. Let's back up for a moment and look at XML Schemas from a "big picture" point of view.
"In a typical program, up to 60% of the code is spent checking the data!" - source unknown eXtensible Markup Language Lecturer: Phan Vo Minh Thang MSc. Continued -->
If your data is structured as XML, and there is a schema, then you can hand the data-checking task off to a schema validator. Thus, your code is reduced by up to 60%!!! Big $$ savings! eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
"P.O. is okay"
Schema Validator
P.O. Schema eXtensible Markup Language (Schema at third-party, neutral web site) Lecturer: Phan Vo Minh Thang MSc.
No Limits
Two slides back we showed the classic use of XML Schemas - to validate your data (so that you don't have to write code to do it) However, there are many other uses for XML Schemas. Schemas are a wonderful source of metadata. Truly, your imagination is the only limit on its usefulness. On the next slide I show how to use the metadata provided by XML Schemas to create a GUI. The slide after that shows how to automatically generate an API using the metadata in XML Schemas. Following that is a slide showing how to create a "smart editor" using XML Schemas.
P.O. Schema
GUI Builder
P.O. HTML
P.O. Schema
API Builder
P.O. API
P.O. Schema
Helps you build your instance documents. For example, it pops up a menu showing you what is valid next. It knows this by looking at the XML Schema!
Regular Expressions
Recall that the string datatype has a pattern facet. The value of a pattern facet is a regular expression. Below are some examples of regular expressions: Regular Expression - Chapter \d - Chapter \d - a*b - [xyz]b - a?b - a+b - [a-c]x Example - Chapter 1 - Chapter 1 - b, ab, aab, aaab, - xb, yb, zb - b, ab - ab, aab, aaab, - ax, bx, cx
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
Example
ax, bx, cx -x, ax, cx ax, cx, -x
any non-digit char followed by x any non-digit char followed by x Chapter followed by a blank followed by a digit
[a-zA-Z-[Ol]]*
\.
<xsd:simpleType name="money"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\p{Sc}\p{Nd}+(\.\p{Nd}\p{Nd})?"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="cost" type="money"/> <cost>$45.99</cost> <cost>300</cost>
Example R.E.
[1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
0 to 99 100 to 199 200 to 249 250 to 255
This regular expression restricts a string to have values between 0 and 255. Such a R.E. might be useful in describing an IP address ...
IP Datatype Definition
<xsd:simpleType name="IP"> <xsd:restriction base="xsd:string"> <xsd:pattern value="(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3} ([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"> <xsd:annotation> <xsd:documentation> Datatype for representing IP addresses. Examples, 129.83.64.255, 64.128.2.71, etc. This datatype restricts each field of the IP address to have a value between zero and 255, i.e., [0-255].[0-255].[0-255].[0-255] Note: in the value attribute (above) the regular expression has been split over two lines. This is for readability purposes only. In practice the R.E. would all be on one line. </xsd:documentation> </xsd:annotation> </xsd:pattern> </xsd:restriction> eXtensible Markup Language </xsd:simpleType>
Do Lab 5
Derived Types
We can do a form of subclassing complexType definitions. We call this "derived types"
derive by extension: extend the parent complexType with more elements derive by restriction: create a type which is a subset of the base type. There are two ways to subset the elements:
redefine a base type element to have a restricted range of values, or redefine a base type element to have a more restricted number of occurrences.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication" > <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Note that BookPublication extends the Publication type, i.e., we are doing Derive by Extension
<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType > <xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType >
Elements declared to be of type BookPublication will have 5 child elements - Title, Author, Date, ISBN, and Publisher. Note that the elements in the derived type are appended to the elements in the base type.
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
ISBN
Date
BookPublication Publisher
Do Lab 6
Derive by Restriction
<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name= "SingleAuthorPublication"> <xsd:complexContent> <xsd:restriction base="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
Elements of type SingleAuthorPublication will have 3 child elements - Title, Author, and Date. However, there must be exactly one Author element. Note that in the restriction type you must repeat all the declarations from the base type (except when the base type has an element with minOccurs="0" and the subtype wishes to delete it. See next slide). eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
Note that in this subtype we have eliminated the Author element, i.e., the subtype is just comprised of an unbounded number of Title elements followed If the base type has an element with minOccurs="0", and the subtype wishes to by a single Date element. not have that element, then it can simply leave it out. eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" minOccurs="0"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name= "ZeroAuthorPublication"> <xsd:complexContent> <xsd:restriction base="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
Prohibiting Derivations
Sometimes we may want to create a type and disallow all derivations of it, or just disallow extension derivations, or disallow restriction derivations.
Rationale: "For example, I may create a complexType and make it publicly available for others to use. However, I don't want them to extend it with their proprietary extensions or subset it to remove, say, copyright information." (Jon Cleaver)
<xsd:complexType name="Publication" final="restriction" > Publication cannot be restricted <xsd:complexType name="Publication" final="extension" > Publication cannot be extended
Definitions: - type (simple, complex) definitions - attribute group definitions - model group definitions
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication" > <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Element Substitution
Oftentimes in daily conversation there are several ways to express something. In Boston we use the words "T" and "subway" interchangeably. For example, "we took the T into town", or "we took the subway into town". Thus, "T" and "subway" are substitutable. Which one is used may depend upon what part of the state you live in, what mood you're in, or any number of factors. We would like to be able to express this substitutability in XML Schemas.
That is, we would like to be able to declare in a schema an element called "subway", an element called "T", and state that "T"may be substituted for "subway". Instance documents can then use either <subway> or <T>, depending on their preference.
substitutionGroup
We can define a group of substitutable elements (called a substitutionGroup) by declaring an element (called the head) and then declaring other elements which state that they are substitutable for the head element.
<xsd:element name="T" substitutionGroup="subway" type="xsd:string"/> subway is the head element T is substitutable for subway So what's the big deal? - Anywhere a head element can be used in an instance document, any member of the substitutionGroup can be substituted!
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
Schema:
<xsd:element name="subway" type="xsd:string"/> <xsd:element name="T" substitutionGroup="subway" type="xsd:string"/> <xsd:element name="transportation"> <xsd:complexType> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType> </xsd:element> <transportation> <subway>Red Line</subway> </transportation> <transportation> <T>Red Line</T> </transportation>
This example shows the <subway> element being substituted with the <T> element.
eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
International Clients
We can use substitutionGroups to create elements customized for our international clients. On the next slide is shown a Spanish version of the element.
Schema:
<xsd:element name="subway" type="xsd:string"/> <xsd:element name="metro" substitutionGroup="subway" type="xsd:string"/> <xsd:complexType name="transport"> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType> <xsd:element name="transportation" type="transport"/> <xsd:element name="transporte" substitutionGroup="transportation"/> <transportation> <subway>Red Line</subway> </transportation>
Instance doc: Alternative instance doc (customized for our Spanish clients):
This type must be the same as "xxx" or, it must be derived from "xxx".
<xsd:element name="Publication" type="PublicationType"/> <xsd:element name="Book" substitutionGroup="Publication" type="BookType"/> <xsd:element name="Magazine" substitutionGroup="Publication" type="MagazineType"/> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Publication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element>
BookType
MagazineType
In order for Book and Magazine to be in a substitutionGroup with Publication, their type (BookType and MagazineType, respectively) must be the same as, or derived from Publication's type (PublicationType)
<xsd:complexType name="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="BookType"> <xsd:complexContent> <xsd:extension base="PublicationType" > <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="MagazineType"> <xsd:complexContent> <xsd:restriction base="PublicationType"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent> </xsd:complexType>
<?xml version="1.0"?> <BookStore > <Book> <Title>Illusions: The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Magazine> <Title>Natural Health</Title> <Date>1999</Date> </Magazine> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper & Row</Publisher> </Book> </BookStore> <BookStore> can contain any element in the substitutionGroup with Publication! eXtensible Markup Language
Schema:
<xsd:element name="subway" type="xsd:string" block="substitution"/> <xsd:element name="T" substitutionGroup="subway"/> <xsd:element name="transportation"> <xsd:complexType> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType> </xsd:element> <transportation> <subway>Red Line</subway> </transportation> <transportation> <T>Red Line</T> </transportation>
Instance doc:
Not allowed!
Note: there is no error in declaring T to be substitutable with subway. The error occurs only when you try to do substitution in the instance document. eXtensible Markup Language
1. Transitive: if element A can substitute for element B, and element B can substitute for element C, then element A can substitute for element C. A --> B --> C then A --> C 2. Non-symmetric: if element A can substitute for element B, it is not the case that element B can substitute for element A.
Do Lab 7
Attributes
On the next slide I show a version of the BookStore DTD that uses attributes. Then, on the following slide I show how this is implemented using XML Schemas.
<!ELEMENT BookStore (Book+)> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> <!ATTLIST Book Category (autobiography | non-fiction | fiction) #REQUIRED InStock (true | false) "false" Reviewer CDATA " "> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)> BookStore.dtd
<xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> <xsd:attributeGroup ref="BookAttributes"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="BookAttributes"> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> Category (autobiography | non-fiction | fiction) #REQUIRED <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> InStock (true | false) "false" <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> Reviewer CDATA " " <xsd:attribute name="Reviewer" type="xsd:string" default=" "/> </xsd:attributeGroup>
(see example07)
<xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute>
"Instance documents are required to have the Category attribute (as indicated by use="required"). The value of Category must be either autobiography, non-fiction, or fiction (as specified by the enumeration facets)."
Note: attributes can only have simpleTypes (i.e., attributes cannot have child elements).
<xsd:attribute name="name" use="how-its-used" default/fixed="value"> <xsd:simpleType> <xsd:restriction base="simple-type"> <xsd:facet value="value"/> </xsd:restriction> eXtensible Markup Language </xsd:simpleType> Lecturer: Phan Vo Minh Thang MSc. </xsd:attribute>
The "use" attribute only makes sense in the context of an element declaration. Example: "for each Book element, the Category attribute is required". When declaring a global attribute do not specify a "use"
<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> </xsd:sequence> <xsd:attribute ref="Category" use="required"/> </xsd:complexType> </xsd:element> <xsd:attribute name="Category"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="fiction"/> <xsd:enumeration value="non-fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute>
Local attribute declaration. Use the "use" attribute here. Global attribute declaration. Must NOT have a "use" ("use" only makes sense in the context of an element)
Inlining Attributes
On the next slide is another way of expressing the last example - the attributes are inlined within the Book declaration rather than being separately defined in an attributeGroup.
<xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> <xsd:attribute name="Reviewer" type="xsd:string" default=" "/> </xsd:complexType> </xsd:element>
<xsd:element name="foo"> <xsd:complexType> <xsd:sequence> </xsd:sequence> <xsd:attribute name="bar" /> <xsd:attribute name="boo" /> </xsd:complexType> eXtensible Markup Language </xsd:element>
These attributes apply to the element they are nested within (Book) That is, Book has three attributes - Category, InStock, and Reviewer.
<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> <xsd:attribute name="Reviewer" type="xsd:string" default=" "/> </xsd:complexType> </xsd:element>
Do Lab 8.a,
Example. Consider this: <elevation units="feet">5440</elevation> The elevation element has these two constraints: - it has a simple (integer) content - it has an attribute called units How do we declare elevation? (see next slide)
<xsd:element name="elevation"> <xsd:complexType> 1 <xsd:simpleContent> 2 <xsd:extension base="xsd:integer"> 3 <xsd:attribute name="units" type="xsd:string" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element>
1. elevation contains an attribute. - therefore, we must use <xsd:complexType> 2. However, elevation does not contain child elements (which is what we generally use <complexType> to indicate). Instead, elevation contains simpleContent. 3. We wish to extend the simpleContent (an integer) ... 4. with an attribute.
<xsd:simpleType name="elevationType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="12000"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="unitsType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="feet"/> <xsd:enumeration value="meters"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="elevation"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="elevationType"> <xsd:attribute name="units" type="unitsType" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element>
<xsd:element name="apple"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="variety" type="xsd:string" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> Example. <apple variety="Cortland">Large, green, sour</apple>
<xsd:complexType name=""> <xsd:complexContent> <xsd:extension base="X"> </xsd:extension> </xsd:complexContent> </xsd:complexType> X must be a complexType
versus
<xsd:complexType name=""> <xsd:simpleContent> <xsd:extension base="Y"> </xsd:extension> </xsd:simpleContent> </xsd:complexType> Y must be a simpleType
Do Lab 8.b, 8.c
group Element
The group element enables you to group together element declarations. Note: the group element is just for grouping together element declarations, no attribute declarations allowed!
<xsd:element name="Book" > <xsd:complexType> <xsd:sequence> <xsd:group ref="PublicationElements"/> <xsd:element name="ISBN" type="string"/> <xsd:element name="Reviewer" type="string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="CD" > <xsd:complexType> <xsd:sequence> <xsd:group ref="PublicationElements"/> <xsd:element name="RecordingStudio" type="string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:group name="PublicationElements"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> </xsd:sequence> </xsd:group>
<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:group name="PublicationElements"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" minOccurs="0"/> <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> </xsd:sequence> </xsd:group> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> ... </xsd:complexType> </xsd:element>
Cannot inline the group definition. Instead, you must use a ref here and define the group globally.
Expressing Alternates
DTD: <!ELEMENT transportation (train | plane | automobile)>
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.travel.org" xmlns="http://www.travel.org" elementFormDefault="qualified"> <xsd:element name="transportation"> <xsd:complexType> <xsd:choice> <xsd:element name="train" type="xsd:string"/> <xsd:element name="plane" type="xsd:string"/> <xsd:element name="automobile" type="xsd:string"/> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema>
XML Schema:
(see example10)
Note: the choice is an exclusive-or, that is, transportation can contain only one element - either train, or plane, or automobile. eXtensible Markup Language
XML Schema:
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.binary.org" xmlns="http://www.binary.org" elementFormDefault="qualified"> <xsd:element name="binary-string"> <xsd:complexType> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element name="zero" type="xsd:unsignedByte" fixed="0"/> <xsd:element name="one" type="xsd:unsignedByte" fixed="1"/> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema>
(see example 11) Notes: 1. An element can fix its value, using the fixed attribute. 2. When you don't specify a value for minOccurs, it defaults to "1". Same for maxOccurs. See the last example (transportation) where we used a <choice> element with no minOccurs or maxOccurs. eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
DTD:
XML Schema:
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.life.org" xmlns="http://www.life.org" elementFormDefault="qualified"> <xsd:element name="life"> <xsd:complexType> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element name="work" type="xsd:string"/> <xsd:element name="eat" type="xsd:string"/> </xsd: sequence> <xsd:choice> <xsd:element name="work" type="xsd:string"/> <xsd:element name="play" type="xsd:string"/> </xsd:choice> <xsd:element name="sleep" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
XML Schema:
(see example 12) <all> means that Book must contain all five child elements, but eXtensible Markup Language Lecturer: Phan Vo Minh Thang MSc. they may occur in any order.
Do Lab 9
Empty Element
DTD: <!ELEMENT image EMPTY> <!ATTLIST image href CDATA #REQUIRED>
Schema:
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.photography.org" xmlns="http://www.photography.org" elementFormDefault="qualified"> <xsd:element name="gallery"> <xsd:complexType> <xsd:sequence> <xsd:element name="image" maxOccurs="unbounded"> <xsd:complexType> <xsd:attribute name="href" type="xsd:anyURI" use="required"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
No targetNamespace (noNamespaceSchemaLocation)
Sometimes you may wish to create a schema but without associating the elements with a namespace. The targetNamespace attribute is actually an optional attribute of <schema>. Thus, if you dont want to specify a namespace for your schema then simply dont use the targetNamespace attribute. Consequences of having no namespace 1. In the instance document dont namespace qualify the elements. 2. In the instance document, instead of using schemaLocation use noNamespaceSchemaLocation.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title"/> <xsd:element ref="Author"/> <xsd:element ref="Date"/> <xsd:element ref="ISBN"/> <xsd:element ref="Publisher"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:schema>
Note that there is no targetNamespace attribute, and note that there is no longer a default namespace.
(see example14)
<?xml version="1.0"?> <BookStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "BookStore.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> </BookStore> (see example14) 1. Note that there is no default namespace declaration. So, none of the elements are associated with a namespace. 2. Note that we do not use xsi:schemaLocation (since it requires a pair of values - a namespace and a URL to the schema for that namespace). Instead, we use xsi:noNamespaceSchemaLocation.
An instance document may be composed of elements from multiple schemas. Validation can apply to the entire XML instance document, or to a single element.
<?xml version="1.0"?> <Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.book.org Book.xsd http://www.employee.org Employee.xsd"> <Books> <Book xmlns="http://www.book.org"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>Macmillan Publishing</Publisher> </Book> <Book xmlns="http://www.book.org"> <Title>Illusions: The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book xmlns="http://www.book.org"> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper & Row</Publisher> </Book> </Books> <Employees> <Employee xmlns="http://www.employee.org"> <Name>John Doe</Name> <SSN>123-45-6789</SSN> </Employee> <Employee xmlns="http://www.employee.org"> <Name>Sally Smith</Name> <SSN>000-11-2345</SSN> </Employee> </Employees> </Library>
The <Book> elements are defined in Book.xsd, and the <Employee> elements are defined in Employee.xsd. The <Library>, <Books>, and <Employees> elements are not defined in any schema! 1. A schema validator will validate each Book element against Book.xsd. 2. It will validate each Employee element against Employee.xsd. 3. It will not validate the other elements.
Lecturer: Phan Vo Minh Thang MSc.
LibraryBook.xsd
LibraryEmployee.xsd
<xsd:schema > <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> </xsd:schema> eXtensible Markup Language Library.xsd Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.library.org" xmlns="http://www.library.org" elementFormDefault="qualified"> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> <xsd:element name="Library"> <xsd:complexType> <xsd:sequence> <xsd:element name="Books"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Employees"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Employee" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
A schema can <include> another schema which has no targetNamespace. The included components take on the targetNamespace of the schema that is doing the <include>. This is called the Chameleon Effect. The components in the no-namespace schema are called Chameleon components.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="Type" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Product.xsd (see example17)
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.company.org" xmlns="http://www.company.org" elementFormDefault="qualified"> <xsd:include schemaLocation="Person.xsd"/> <xsd:include schemaLocation="Product.xsd"/> <xsd:element name="Company"> <xsd:complexType> <xsd:sequence> <xsd:element name="Person" type="Person" maxOccurs="unbounded"/> <xsd:element name="Product" type="ProductType" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Company.xsd (see example17) This schema <include>s Product.xsd. Thus, the components in Product.xsd are namespace-coerced to the company targetNamespace. Consequently, we can reference those components just as though they had originally been declared in a schema with the same targetNamespace.
Namespace A A.xsd <xsd:schema > <xsd:import namespace="A" schemaLocation="A.xsd"/> <xsd:import namespace="B" schemaLocation="B.xsd"/> </xsd:schema> C.xsd
Namespace B B.xsd
Camera Schema
Nikon.xsd
Olympus.xsd
Pentax.xsd
Camera.xsd
<?xml version=1.0> <material> XML Lectures Notes <section id=05> XML Schemas </section> </material> <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.nikon.com" xmlns="http://www.nikon.com" elementFormDefault="qualified"> <xsd:complexType name="body_type"> Nikon.xsd <xsd:sequence> <xsd:element name="description" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.olympus.com" xmlns="http://www.olympus.com" elementFormDefault="qualified"> <xsd:complexType name="lens_type"> <xsd:sequence> <xsd:element name="zoom" type="xsd:string"/> <xsd:element name="f-stop" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.pentax.com" xmlns="http://www.pentax.com" elementFormDefault="qualified"> <xsd:complexType name="manual_adapter_type"> <xsd:sequence> <xsd:element name="speed" type="xsd:string"/> </xsd:sequence> eXtensible Markup Language </xsd:complexType> Lecturer: Phan Vo Minh Thang MSc. </xsd:schema>
Olympus.xsd
Pentax.xsd
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" elementFormDefault="qualified"> These import <xsd:import namespace="http://www.nikon.com" schemaLocation="Nikon.xsd"/> elements give <xsd:import namespace="http://www.olympus.com" us access to schemaLocation="Olympus.xsd"/> the components <xsd:import namespace="http://www.pentax.com" in these other schemaLocation="Pentax.xsd"/> schemas. <xsd:element name="camera"> <xsd:complexType> <xsd:sequence> <xsd:element name="body" type="nikon:body_type"/> <xsd:element name="lens" type="olympus:lens_type"/> <xsd:element name="manual_adapter" type="pentax:manual_adapter_type"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:schema>
<?xml version="1.0"?> <c:camera xmlns:c="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd http://www.nikon.com Nikon.xsd http://www.olympus.com Olympus.xsd http://www.pentax.com Pentax.xsd"> <c:body> <nikon:description>Ergonomically designed casing for easy handling</nikon:description> </c:body> <c:lens> <olympus:zoom>300mm</olympus:zoom> <olympus:f-stop>1.2</olympus:f-stop> </c:lens> <c:manual_adapter> <pentax:speed>1/10,000 sec to 100 sec</pentax:speed> </c:manual_adapter> </c:camera>
The Camera instance uses elements from the Nikon, Olympus, and Pentax namespaces.
Redundant!
On the previous slide, the value of schemaLocation contained four pairs of values - one for camera, and three for each schema that it uses. The later three are redundant. Once you give the schemavalidator the URL to the camera schema it will examine the camera schema and see the import elements, thus it will deduce the other schemas being used (Nikon, Olympus, and Pentax) The next slide shows the non-redundant version.
<?xml version="1.0"?> <c:camera xmlns:c="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd"> <c:body> <nikon:description>Ergonomically designed casing for easy handling</nikon:description> </c:body> <c:lens> <olympus:zoom>300mm</olympus:zoom> <olympus:f-stop>1.2</olympus:f-stop> </c:lens> <c:manual_adapter> <pentax:speed>1/10,000 sec to 100 sec</pentax:speed> </c:manual_adapter> </c:camera>
eXtensible Markup
Creating Lists
There are times when you will want an element to contain a list of values, e.g., "The contents of the Numbers element is a list of numbers".
Example: For a document containing a Lottery drawing we might have <Numbers>12 49 37 99 20 67</Numbers> How do we declare the element Numbers ... (1) To contain a list of integers, and (2) Each integer is restricted to be between 1 and 99, and (3) The total number of integers in the list is exactly six. eXtensible Markup Language
<?xml version="1.0"?> <LotteryDrawings xmlns="http://www.lottery.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.lottery.org Lottery.xsd"> <Drawing> <Week>July 1</Week> <Numbers>21 3 67 8 90 12</Numbers> </Drawing> <Drawing> <Week>July 8</Week> <Numbers>55 31 4 57 98 22</Numbers> </Drawing> <Drawing> <Week>July 15</Week> <Numbers>70 77 19 35 44 11</Numbers> </Drawing> </LotteryDrawings>
Lottery.xml (see example19) eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.lottery.org" xmlns="http://www.lottery.org" elementFormDefault="qualified"> <xsd:simpleType name="LotteryNumbers"> <xsd:list itemType="xsd:positiveInteger"/> </xsd:simpleType> <xsd:element name="LotteryDrawings"> <xsd:complexType> <xsd:sequence> <xsd:element name="Drawing" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Week" type="xsd:string"/> <xsd:element name="Numbers" type="LotteryNumbers"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Lottery.xsd eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<?xml version=1.0> <material> XML Lectures Notes <section id=05> XML Schemas </section> </material> <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.lottery.org" xmlns="http://www.lottery.org" elementFormDefault="qualified"> <xsd:simpleType name="OneToNinetyNine"> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxInclusive value="99"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="NumbersList"> <xsd:list itemType="OneToNinetyNine"/> </xsd:simpleType> <xsd:simpleType name="LotteryNumbers"> <xsd:restriction base="NumbersList"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="LotteryDrawings"> <xsd:complexType> <xsd:sequence> <xsd:element name="Drawing" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Week" type="xsd:string"/> <xsd:element name="Numbers" type="LotteryNumbers"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
<xsd:simpleType name="OneToNinetyNine"> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxInclusive value="99"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="NumbersList"> <xsd:list itemType="OneToNinetyNine"/> </xsd:simpleType> <xsd:simpleType name="LotteryNumbers"> <xsd:restriction base="NumbersList"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType>
NumbersList is a list where the type of each item is OneToNinetyNine. LotteryNumbers restricts NumbersList to a length of six (i.e., an element declared to be of type LotteryNumbers must hold a list of numbers, between 1 and 99, and the length of the list must be exactly six).
<xsd:simpleType name="OneToNinetyNine"> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxInclusive value="99"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="NumbersList"> <xsd:list itemType="OneToNinetyNine"/> </xsd:simpleType> <xsd:simpleType name="LotteryNumbers"> <xsd:restriction base="NumbersList"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType>
Alternatively,
<xsd:simpleType name="LotteryNumbers"> <xsd:restriction> <xsd:simpleType> <xsd:list itemType="OneToNinetyNine"/> </xsd:simpleType> <xsd:length value="6"/> </xsd:restriction> </xsd:simpleType> This is read as: "We are creating a new type called LotteryNumbers. It is a restriction. At this point we can either use the base attribute or a simpleType child element to indicate the type that we are restricting (you cannot use both the base attribute and the simpleType child element). We want to restrict the type that is a list of OneToNinetyNine. We will restrict that type to a length of 6."
In the instance document, you must separate each item in a list with white space (blank space, tab, or carriage return) The only facets that you may use with a list type are:
length: use this to specify the length of the list minLength: use this to specify the minimum length of the list maxLength: use this to specify the maximum length of the list enumeration: use this to specify the values that the list may have pattern: use this to specify the values that the list may have
Do Lab 11.d
simpleType 1
simpleType 2
simpleType 1 + simpleType 2
Note: you can create a union of more than just two simpleTypes eXtensible Markup Language
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.CostelloReunion.org" xmlns="http://www.CostelloReunion.org" elementFormDefault="qualified"> <xsd:simpleType name="Parent"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Mary"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="PatsFamily"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Pat"/> <xsd:enumeration value="Patti"/> <xsd:enumeration value="Christopher"/> <xsd:enumeration value="Elizabeth"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="BarbsFamily"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Barb"/> <xsd:enumeration value="Greg"/> <xsd:enumeration value="Dan"/> <xsd:enumeration value="Kimberly"/> </xsd:restriction> </xsd:simpleType>
<xsd:simpleType name="JudysFamily"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Judy"/> <xsd:enumeration value="Peter"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="TomsFamily"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Tom"/> <xsd:enumeration value="Cheryl"/> <xsd:enumeration value="Marc"/> <xsd:enumeration value="Joe"/> <xsd:enumeration value="Brian"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="RogersFamily"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Roger"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="JohnsFamily"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="John"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="CostelloFamily"> <xsd:union memberTypes="Parent PatsFamily BarbsFamily JudysFamily TomsFamily RogersFamily JohnsFamily"/> </xsd:simpleType>
<xsd:element name="Y2KFamilyReunion"> <xsd:complexType> <xsd:sequence> <xsd:element name="Participants"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name" type="CostelloFamily" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
<?xml version="1.0"?> <Y2KFamilyReunion xmlns="http://www.CostelloReunion.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.CostelloReunion.org Y2KFamilyReunion.xsd"> <Participants> <Name>Mary</Name> <Name>Pat</Name> <Name>Patti</Name> <Name>Christopher</Name> <Name>Elizabeth</Name> <Name>Judy</Name> <Name>Peter</Name> <Name>Tom</Name> <Name>Cheryl</Name> <Name>Marc</Name> <Name>Joe</Name> <Name>Roger</Name> </Participants> </Y2KFamilyReunion>
eXtensible Markup Language Y2KFamilyReunion.xml (see example 20)
Lecturer: Phan Vo Minh Thang MSc.
Alternative
<xsd:simpleType name="CostelloFamily"> <xsd:union> <xsd:simpleType> <xsd:restriction base="xsd:string"> A union of <xsd:enumeration value="Mary"/> anonymous </xsd:restriction> simpleTypes </xsd:simpleType> <xsd:simpleType> The disadvantage of <xsd:restriction base="xsd:string"> creating the union <xsd:enumeration value="Pat"/> type in this manner <xsd:enumeration value="Patti"/> is that none of the <xsd:enumeration value="Christopher"/> simpleTypes are <xsd:enumeration value="Elizabeth"/> reusable. </xsd:restriction> </xsd:simpleType> </xsd:union> </xsd:simpleType> eXtensible Version 2 of Y2KFamilyReunion.xsd (see example 21) Markup Language
Alternatively, <xsd:simpleType name="name"> <xsd:union> <xsd:simpleType> </xsd:simpleType> <xsd:simpleType> </xsd:simpleType> </xsd:union> </xsd:simpleType> eXtensible Markup Language
Lecturer: Phan Vo Minh Thang MSc.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.maxOccurs.org" xmlns="http://www.maxOccurs.org" elementFormDefault="qualified"> <xsd:simpleType name="unbounded_type"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="unbounded"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="maxOccurs_type"> <xsd:union memberTypes="unbounded_type xsd:nonNegativeInteger"/> </xsd:simpleType> <xsd:element name="schema"> <xsd:complexType> <xsd:sequence> <xsd:element name="element"> <xsd:complexType> <xsd:attribute name="maxOccurs" type="maxOccurs_type" default="1"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
1. simpleType that uses a built-in base type: <xsd:simpleType name= "EarthSurfaceElevation"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction> </xsd:simpleType>
2. simpleType that uses another simpleType as the base type: <xsd:simpleType name= "BostonSurfaceElevation"> <xsd:restriction base="EarthSurfaceElevation"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> </xsd:restriction> </xsd:simpleType>
any Element
The <any> element enables the instance document author to extend his/her document with elements not specified by the schema.
<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> <xsd:any minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element>
Now an instance document author can optionally extend (after <Publisher>) the content of <Book> elements with any element. eXtensible Markup Language
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.repository.org" xmlns="http://www.repository.org" elementFormDefault="qualified"> <xsd:element name="Reviewer"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name"> <xsd:complexType> <xsd:sequence> <xsd:element name="First" type="xsd:string"/> <xsd:element name="Last" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
Suppose that the instance document author discovers this schema repository, and wants to extend his/her <Book> elements with a <Reviewer> element. He/she can do so! Thus, the instance document will be extended with an element never anticipated by the schema author. Wow! eXtensible Markup Language
<?xml version="1.0"?> <BookStore xmlns="http://www.BookRetailers.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www. BookRetailers.org This instance document BookSeller.xsd uses components from http://www. repository.org two different schemas. SchemaRepository.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> <Reviewer xmlns="http://www.repository.org"> <Name> <First>Roger</First> <Last>Costello</Last> </Name> </Reviewer> </Book> <Book> <Title>Illusions: The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> eXtensible Markup Language </BookStore> Lecturer: Phan Vo Minh Thang MSc.
<any namespace="##other"/> allows the instance document to contain a new element, provided the element comes from a namespace other than the one the schema is defining (i.e., targetNamespace). <any namespace="http://www.somewhere.com"/> allows a new element, provided it's from the specified namespace Note: you can specify a list of namespaces, separated by a blank space. One of the namespaces can be ##targetNamespace (see next) <any namespace="##targetNamespace"/> allows a new element, provided it's from the namespace that the schema is defining. <any namespace="##any"/> allows an element from any namespace. This is the default. <any namespace="##local"/> the new element must come from no namespace eXtensible Markup Language
anyAttribute
The <anyAttribute> element enables the instance document author to extend his/her document with attributes not specified by the schema.
<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> <xsd:any minOccurs="0"/> </xsd:sequence> <xsd:anyAttribute/> </xsd:complexType> </xsd:element>
Now an instance document author can add any number of attributes onto a eXtensible Markup Language <Book> element (as well as extend the element content).
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.repository.org" xmlns="http://www.repository.org" elementFormDefault="qualified"> <xsd:element name="Reviewer"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name"> <xsd:complexType> <xsd:sequence> <xsd:element name="First" type="xsd:string"/> <xsd:element name="Last" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:attribute name="id" type="xsd:ID"/> </xsd:schema>
SchemaRepository.xsd (see example24) Suppose that the instance document author discovers this schema, and wants to extend his/her <Book> elements with an id attribute. He/she can do so! Thus, the instance document will be extended with an attribute never anticipated by the schema author. Wow! eXtensible Markup Language
<?xml version="1.0"?> <BookSeller xmlns="http://www.BookRetailers.org" xmlns:sr="http://www.repository.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.BookRetailers.org BookSeller.xsd http://www.repository.org SchemaRepository.xsd"> <Book sr:id="P.M."> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>McMillin Publishing</Publisher> <Reviewer xmlns="http://www.repository.org"> <Name> <First>Roger</First> <Last>Costello</Last> </Name> </Reviewer> </Book> <Book sr:id="R.B."> <Title>Illusions: The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> </BookSeller>
With the <any> and <anyAttribute> elements we can design our schemas with the recognition that, as schema designers, we can never anticipate all the different kinds of data that instance document authors will want to use in the instance document. That is, we are smart enough to know that we're not smart enough to know all the different data instance document authors will require.
Open Content
Definition: an open content schema is one that allows instance documents to contain additional elements beyond what is declared in the schema. This is achieved by using the <any> and <anyAttribute> elements in the schema. Sprinkling <any> and <anyAttribute> elements liberally throughout your schema will yield benefits in terms of how evolvable your schema is. See later slides for how open content enables the rapid evolution of schemas that is required in today's marketplace.
Global Openness
There is a range of openness that a schema may support - anywhere from having instance documents where new elements can be inserted anywhere (global openness), to instance documents where new elements can be inserted only at specific locations (localized openness)
<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> This schema is allowing <xsd:any minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="Title"> expansion before and <xsd:complexType> <xsd:simpleContent> after every element. <xsd:extension base="xsd:string"> Further, it is allowing <xsd:anyAttribute/> </xsd:extension> for attribute expansion </xsd:simpleContent> on every element. </xsd:complexType> <xsd:/element> <xsd:any minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="Author"> Truly, this is the ultimate <xsd:complexType> in openness! <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:anyAttribute/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> ... </xsd:sequence> <xsd:anyAttribute/> eXtensible Markup Language </xsd:complexType> Lecturer: Phan Vo Minh Thang MSc. <xsd:/element>
Localized Openness
With localized openness we design our schema to allow instance documents to extend only at specific points in the document
<xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> <xsd:any minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element>
With this schema we are allowing instance documents to extend only at the end of Book's content model.
Capture the semantics in the XML Schema Describe the semantics within the <annotation> element Adopt the convention that every element and attribute have an annotation which provides information on the meaning
Advantages: The XML Schema will capture the data structure, meta-data, and relationships between the elements Use of strong typing will capture much of the data content The annotations can capture definitions and other explanatory information The structure of the "definitions" will always be consistent with the structure used in the schema since they are linked Since the schema itself is an XML document, we can use XSLT to extract the annotations and transform the "semantic" information into a format suitable for human consumption eXtensible Markup Language
Requirement
Capture data structure and content
Inherent part of XML schema Use XML annotation element to capture data definitions Use data attributes to capture meta-data
Make data, meta-data and semantics accessible to a wide variety of clients Ensure parallelism between data structure definition and data semantics
Use XSL to transform this XML tagged data to make it readable to humans or other computers Consistency ensured by having both part of same XML schema
Info
Course name: