Week 4 - More XSD: Internet Technologies and Web Services
Week 4 - More XSD: Internet Technologies and Web Services
Week 4 - More XSD: Internet Technologies and Web Services
• <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>
Mixed Content
• Sometimes an element will contain both child
elements and character text.
• For example, a para element might contain
mostly plain character text, but it could also have
other elements (e.g, emphasis) littered
throughout the character text.
<para>Some text here
<emphasis>some more text</emphasis>
</para>
In xsd: mixed=“true” in <complexType>
Example code for Mixed Content
• <?xml version="1.0"?> <Employee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:noNamespaceSchemaLocation="Employee4.xsd">
<Name> <FirstName>Paul</FirstName>
<LastName>McCartney</LastName> </Name>
<Salary>90000</Salary> <Bio> Worked for
<Company>the Beatles</Company> as a
<JobTitle>Singer</JobTitle>. Worked for
<Company>the Beatles</Company> as a
<JobTitle>Bass Guitarist</JobTitle>. Worked for
<Company>the Wings</Company> as a
<JobTitle>Singer</JobTitle>. </Bio> </Employee>
Restrictions
• Restrictions are used to define acceptable
values for XML elements or attributes.
Restrictions on XML elements are called
facets.
• Restrictions can be on:
– Values
– Set of Values
– Series of Values
– Length
Restriction on Values
• The following example defines an element called
"age" with a restriction. The value of age cannot
be lower than 0 or greater than 120:
• <xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restriction on Set of Values
• To limit the content of an XML element to a
set of acceptable values, we would use the
enumeration constraint.
• The example that follows defines an element
called "car" with a restriction. The only
acceptable values are: Audi, Golf, BMW
Example Code – Option 1
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example Code – Option 2
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
Restriction on Series of Values
• The example below defines an element called
"letter" with a restriction. The acceptable value is
zero or more occurrences of lowercase letters
from a to z:
• <xs:element name="letter“>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length
• To limit the length of a value in an element,
we would use the length, maxLength, and
minLength constraints.
• The examples that follow define an element
called "password" with a restriction.
– The value must be exactly eight characters
– The value must be minimum five characters and
maximum eight characters
Example 1
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 2
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Global Elements
• Like types, elements can be defined globally.
The same rule apply for global items, i.e.,
element must be a child of the <xs:schema>.
• Make the Lecturer element global and provide
a reference to it.
• Occurrence constraints are usually added to
the reference.
Global Elements (2)
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element ref="Lecturer" minOccurs="1"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The Lecturer Global Element
<xs:element name="Lecturer">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="CharsOnly"/>
<xs:element name="Surname" type="CharsOnly"/>
<xs:choice>
<xs:element name="OfficeAddress" type="OfficeAddressType"/>
<xs:element name="ContactDetails" type="ContactDetailsType"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="Mr|Miss|Ms|Mrs"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
Question – write the schema for the
xml file below
<?xml version="1.0" encoding="utf-8"?>
<Modules
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=“question.xsd">
<Module code="CSE2041">
<Name shortName="Web 2">Web Technologies II</Name>
<Level>2</Level>
<ResourcePerson>
<FullName>John Smith</FullName>
</ResourcePerson>
</Module>
<Module code="CSE1244">
<Name shortName=“ABCD">Some Module Name</Name>
<Level>1</Level>
<ResourcePerson>
<Name>Janet</Name>
<Surname>Higgins</Surname>
</ResourcePerson>
</Module>
</Modules>
References
• Examples come from:
http://www.learn-xml-schema-
tutorial.com/Complex-Type-Elements.cfm
• http://www.w3schools.com/
• http://www.liquid-technologies.com/