CSC570 - Chapter 7 - XSD PDF
CSC570 - Chapter 7 - XSD PDF
CSC570 - Chapter 7 - XSD PDF
PROGRAMMING
XML Schemas
XML Schemas
• One of the greatest strength of XML Schemas is the support for data
types.
• It is easier to describe allowable document content
• It is easier to validate the correctness of data
• It is easier to define data facets (restrictions on data)
• It is easier to define data patterns (data formats)
• It is easier to convert data between different data types
Referring to a schema
• To refer to a DTD in an XML document, the reference goes before the root
element:
• <?xml version="1.0"?>
<!DOCTYPE rootElement SYSTEM "url">
<rootElement> ... </rootElement>
• To refer to an XML Schema in an XML document, the reference goes in the
root element:
• <?xml version="1.0"?>
<rootElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
(The XML Schema Instance reference is required)
xsi:noNamespaceSchemaLocation="url.xsd">
(This is where your XML Schema definition can be found)
...
</rootElement>
Example
<?xml version="1.0“ encoding=“UTF-8”?>
<addresses
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=“test.xsd”>
<addresss>
<name>Joe Tester</name>
<street>Baker Street 5</street>
</address>
</addresses>
The XSD document
• The file extension is .xsd
• The root element is <schema>
• The XSD starts like this:
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.rg/2001/XMLSchema">
Example
<schema>
where:
• name is the name of the element
• the most common values for type are
xs:boolean xs:integer
xs:date xs:string
xs:decimal xs:time
• Other attributes a simple element may have:
• default="default value" if no other value is specified
• fixed="value" no other value may be
specified
Simple element
a) <name>Lily</name>
b) <price>19.90</price>
Defining an attribute
• If an element has attributes, it is considered to be of a complex type.
• Attributes themselves are always declared as simple types
• An attribute is defined as
<xs:attribute name="name" type="type" />
where:
• name and type are the same as for xs:element
• Other attributes a simple element may have:
• default="default value" if no other value is specified
• fixed="value" no other value may be specified
• use="optional" the attribute is not required (default)
• use="required" the attribute must be present
• Remember that attributes are always simple types
Defining an attribute
• Here is an XML element with an attribute:
<lastname lang="EN">Smith</lastname>
a) <name scientific_name=“Lilium”>Lily</name>
b) <price currency=“RM”>19.90</price>
Restrictions, or “facets”
• The general form for putting a restriction on a text value is:
• <xs:element name="name">
<xs:simpleType>
<xs:restriction base="type">
... the restrictions ...
</xs:restriction>
<xs:simpleType>
</xs:element>
Restrictions, or “facets”
• For example:
<xs:element name=“shoe_size">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value=“3"/>
<xs:maxInclusive value=“10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on numbers
Restrictions Details
minInclusive number must be ≥ the given value
fractionDigits number must have no more than value digits after the
decimal point
Example Restriction on Number
<letter>a</letter>
<choice>x</choice>
<car>Audi</car>
Example Restriction on String
<letter>aAbB</letter>
<gender>male</gender>
<password>an1si23A</password>
Complex elements
• A complex element contains other elements and/or
attributes.
• There are 4 kinds of complex elements:
• empty elements
• elements that contain only other elements
• elements that contain only text
• elements that contain both other elements and
text
• Note: Each of these elements may contain
attributes as well!
Examples of Complex Elements
A complex XML element, "product", which A complex XML element, "employee", which
is empty: contains only other elements:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Complex elements - all
• We can also declare complex type without specifying the order.
• xs:all allows elements to appear in any order
<xs:element name="employee">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element
• Despite the name, the members of an xs:all group can occur
once or not at all
• Use minOccurs="n" and maxOccurs="n" to specify how many times
an element may occur (default value is 1)
• In this context, n may only be 0 or 1
Elements Only
<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="basetype">
....
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Text-Only Elements
• OR
<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="basetype">
....
....
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<shoesize country="france">35</shoesize>
<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Text-Only Elements
<marks grade = "A" >90</student>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
• Element Group
<xs:group name="groupname">
...
</xs:group>
• Attribute Group
<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>
<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>
Predefined string types
• Recall that a simple element is defined as:
<xs:element name="name" type="type" />
The End