0% found this document useful (0 votes)
43 views31 pages

Week 4 - More XSD: Internet Technologies and Web Services

The document discusses XML schema (XSD) including: - Occurrence indicators like minOccurs and maxOccurs that specify how many times an element can appear - Content models like sequence, choice, and all that specify the order of child elements - Data types like string and decimal - Complex types that can contain child elements, attributes, or mixed content - Restrictions that define allowed values for elements using facets like enumeration, length, and pattern - Global elements that are defined outside of other elements for reuse

Uploaded by

splokbov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views31 pages

Week 4 - More XSD: Internet Technologies and Web Services

The document discusses XML schema (XSD) including: - Occurrence indicators like minOccurs and maxOccurs that specify how many times an element can appear - Content models like sequence, choice, and all that specify the order of child elements - Data types like string and decimal - Complex types that can contain child elements, attributes, or mixed content - Restrictions that define allowed values for elements using facets like enumeration, length, and pattern - Global elements that are defined outside of other elements for reuse

Uploaded by

splokbov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Week 4 - More XSD

Internet Technologies and Web


Services
Some Rules – Occurrence Indicators
• By default, elements that are declared locally
must show up once and only once within their
parent.
• This constraint can be changed using the
minOccurs and maxOccurs attributes.
• The default value of each of these attributes is 1.
• The value of minOccurs can be any non-negative
integer.
• The value of maxOccurs can be any positive
integer or unbounded, meaning that the element
can appear an infinite number of times.
Recall: XSD Skeleton
<xs:element name="ElementName">
<xs:complexType>
<!--Content Model Goes Here-->
</xs:complexType>
</xs:element>
Content Model
• Content models are made up of model groups.
The three types of model groups are listed
below. These are order indicators.
– xs:sequence - the elements must appear in the
order specified.
– xs:all - the elements must appear, but order is not
important.
– xs:choice - only one of the elements can appear.
Example 1 – xs:sequence
<xs:element name="ElementName">
<xs:complexType>
<xs:sequence>
<xs:element name="Child1" type="xs:string"/>
<xs:element name="Child2" type="xs:string"/>
<xs:element name="Child3" type="xs:string"/>
</xs:sequence>
</xs:complexType> </xs:element>
Example 2 – xs:All
<xs:element name="ElementName">
<xs:complexType>
<xs:all>
<xs:element name="Child1" type="xs:string"/>
<xs:element name="Child2" type="xs:string"/>
<xs:element name="Child3" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
Example 3 – xs:choice
<xs:element name="ElementName">
<xs:complexType>
<xs:choice>
<xs:element name="Child1" type="xs:string"/>
<xs:element name="Child2" type="xs:string"/>
<xs:element name="Child3" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
Exercise 1 – Write a valid XML for the
following XSD
<?xml version="1.0"?> <xs:schema …">
<xs:simpleType name="Salary">
<xs:restriction base="xs:decimal"> <xs:minInclusive value="10000"/>
<xs:maxInclusive value="90000"/>
</xs:restriction> </xs:simpleType>
<xs:element name="Employee"> <xs:complexType> <xs:sequence>
<xs:element name="Name"> <xs:complexType> <xs:sequence>
<xs:element name="FirstName"/> <xs:element name="LastName"/>
</xs:sequence> </xs:complexType> </xs:element> <xs:choice>
<xs:element name="Salary" type="Salary"/> <xs:element
name="Wage" type="xs:decimal"/> </xs:choice> </xs:sequence>
</xs:complexType> </xs:element> </xs:schema>
Model Answer1
<?xml version="1.0"?> <Employee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:noNamespaceSchemaLocation="Employee.xsd">
<Name>
<FirstName>Dave</FirstName>
<LastName>Smith</LastName>
</Name>
<Salary>90000</Salary>
</Employee>
Model Answer 2
<?xml version="1.0"?> <Employee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:noNamespaceSchemaLocation="Employee.xsd">
<Name>
<FirstName>Jill</FirstName>
<LastName>Smith</LastName>
</Name>
<Wage>20.50</Wage>
</Employee>
Complex Elements
• Complex elements may contain:
– Elements Only
– Elements and Attributes
– Attribute with Text Only
• <level id=“1”>65</Level> - complex and contains text
– Mixed Content – Element, Attribute and Text
Complex Types – text only
• This type contains only simple content (text and
attributes), therefore we add a simpleContent element
around the content.
• <xs:element name=“level">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="basetype">
....
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Example code
• <xs:element name=“level">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base=“xs:number">
....
....
</xs:restriction>
</xs:simpleContent>
<xs:attribute name=“id” type=“…”/>
</xs:complexType>
</xs:element>
Example xml and xsd
• <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>
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/

You might also like