UNIT 2C and Lab 6 XML Schema
UNIT 2C and Lab 6 XML Schema
An XML Schema:
defines elements that can appear in a
document
defines attributes that can appear in a
document
defines which elements are child elements
defines the order of child elements
defines the number of child elements
defines whether an element is empty or can
include text
defines data types for elements and attributes
defines default and fixed values for elements
and attributes
XSDHowTo?
Look at this simple XML document
called "note.xml":
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this
weekend!</body>
</note>
9
An XML Schema
ThefollowingexampleisanXMLSchemafilecalled"note.xsd"that
definestheelementsoftheXMLdocumentabove("note.xml"):
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
10
XSD-The<schema>Element
The<schema>elementistheroot
elementofeveryXMLSchema:
< ?xm lversion= "1.0"?>
12
Framgments
targ etN am esp ace=
h ttp ://w w w .w 3sch ools.com
indicatesthattheelementsdefinedbythisschema(note,to,from,
heading,body.)comefromthe"http://www.w3schools.com"
namespace.
elementFormDefault="qualified
indicates that any elements used by the XML
instance document which were declared in this
schema must be namespace qualified.
13
XSDSimpleElements
A simple element is an XML element that can
contain only text. It cannot contain any other
elements or attributes.
However, the "only text" restriction is quite
misleading. The text can be of many different
types. It can be one of the types included in the
XML Schema definition (boolean, string, date,
etc.), or it can be a custom type that you can
define yourself.
You can also add restrictions (facets) to a data
type in order to limit its content, or you can
require the data to match a specific pattern. 14
Example
Here are some XML elements:
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
And here are the corresponding
simple element definitions:
<xs:element name="lastname"
type="xs:string"/>
<xs:element name="age"
type="xs:integer"/>
<xs:element name="dateborn"
16
XSDAttributes
What is an Attribute?
Simple elements cannot have attributes. If an element has
attributes, it is considered to be of a complex type. But the
attribute itself is always declared as a simple type.
How to Define an Attribute?
Thesyntaxfordefininganattributeis:
< xs:attribute nam e= "xxx" type= "yyy"/>
wherexxxisthenameoftheattributeandyyyspecifiesthe
datatypeoftheattribute.
XML Schema has a lot of built-in data types. The most
commontypesare:xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Example
Here is an XML element with an attribute:
<lastname
lang="EN">Smith</lastname>
And here is the corresponding attribute18
definition:
20
Restrictions on Content
When an XML element or attribute has a
data type defined, it puts restrictions on
the element's or attribute's content.
If an XML element is of type "xs:date" and
contains a string like "Hello World", the
element will not validate.
With XML Schemas, you can also add your
own restrictions to your XML elements and
attributes. These restrictions are called
facets.You can read more about facets in
the next chapter.
21
XSDRestrictions/Facets
Restrictions are used to define acceptable values
for XML elements or attributes. Restrictions on
XML elements are called facets.
Restrictions 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>
22
</xs:element>
Restrictions on Length
To limit the length of a value in an
element, we would use the length,
maxLength, and minLength constraints.
This example defines an element called
"password" with a restriction. The value
must be exactly eight characters:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
34
</xs:element>
Description
enumeration
Definesalistofacceptablevalues
fractionDigits
Specifiesthemaximumnumberofdecimalplacesallowed.Mustbeequaltoor
greaterthanzero
length
Specifiestheexactnumberofcharactersorlistitemsallowed.Mustbeequal
toorgreaterthanzero
maxExclusive
Specifiestheupperboundsfornumericvalues(thevaluemustbelessthan
thisvalue)
maxInclusive
Specifiestheupperboundsfornumericvalues(thevaluemustbelessthanor
equaltothisvalue)
maxLength
Specifiesthemaximumnumberofcharactersorlistitemsallowed.Mustbe
equaltoorgreaterthanzero
minExclusive
Specifiesthelowerboundsfornumericvalues(thevaluemustbegreaterthan
thisvalue)
minInclusive
Specifiesthelowerboundsfornumericvalues(thevaluemustbegreaterthan
orequaltothisvalue)
minLength
Specifiestheminimumnumberofcharactersorlistitemsallowed.Mustbe
equaltoorgreaterthanzero
pattern
Definestheexactsequenceofcharactersthatareacceptable
totalDigits
Specifiestheexactnumberofdigitsallowed.Mustbegreaterthanzero
whiteSpace
Specifieshowwhitespace(linefeeds,tabs,spaces,andcarriagereturns)is
35
handled
XSDComplex Elements
40
41
XSDEmptyElements
Complex Empty Elements
An empty XML element:
<product prodid="1345" />
The "product" element above has no content at all. To
define a type with no content, we must define a type that
allows elements in its content, but we do not actually
declare any elements, like this:
<xs:element name="product">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:integer">
<xs:attribute name="prodid"
type="xs:positiveInteger"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
42
</xs:element>
43
XSDElementsOnly
An "elements-only" complex type contains an element that
contains only other elements.
Complex Types Containing Elements Only
An XML element, "person", that contains only other elements:
<person>
<firstname>John</firstname>
<lastname>Smith</lastname>
</person>
You can define the "person" element in a schema, like this:
<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>
Notice the <xs:sequence> tag. It means that the elements defined
("firstname" and "lastname") must appear in that order inside a 44
45
XSDText-OnlyElements
Complex Text-Only Elements
This type contains only simple content (text and attributes), therefore we add a simpleContent
element around the content. When using simple content, you must define an extension OR a
restriction within the simpleContent element, like this:
<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="basetype">
....
....
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
OR
<xs:element name="somename">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="basetype">
....
....
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
46
Tip:Use the extension/restriction element to expand or to limit the base simple type for the element.
47
XSDMixedContent
A mixed complex type element can contain attributes, elements, and
text.
Complex Types with Mixed Content
An XML element, "letter", that contains both text and other elements:
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
The following schema declares the "letter" element:
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
48
49
XSDIndicators
There are seven indicators:
Order indicators:
All
Choice
Sequence
Occurrence indicators:
maxOccurs
minOccurs
Group indicators:
Group name
attributeGroup name
50
Order Indicators
Order indicators are used to define the order of the elements.
All Indicator
The <all> indicator specifies that the child elements can appear
in any order, and that each child element must occur only once:
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
Note:When using the <all> indicator you can set the
<minOccurs> indicator to 0 or 1 and the <maxOccurs>
indicator can only be set to 1 (the <minOccurs> and
<maxOccurs> are described later).
51
Choice Indicator
Sequence Indicator
The <sequence> indicator specifies that the
child elements must appear in a specific order:
<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>
53
Occurrence Indicators
Occurrence indicators are used to define how often an
element can occur.
Note:For all "Order" and "Group" indicators (any, all, choice,
sequence, group name, and group reference) the default
value for maxOccurs and minOccurs is 1.
maxOccurs Indicator
The <maxOccurs> indicator specifies the maximum number
of times an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>
54
The example above indicates that the "child_name" element can occur a
minimum of zero times and a maximum of ten times in the "person" element.
Tip:To allow an element to appear an unlimited number of times, use the
maxOccurs="unbounded" statement:
A working example:
An XML file called "Myfamily.xml":
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
<person>
<full_name>Stale Refsnes</full_name>
</person>
56
The XML file above contains a root element named "persons". Inside this root
element we have defined three "person" elements. Each "person" element must
contain a "full_name" element and it can contain up to five "child_name"
elements.
Here is the schema file "family.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
minOccurs="0" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
57
Group Indicators
Group indicators are used to define related sets of elements.
Element Groups
Element groups are defined with the group declaration, like
this:
<xs:group name="groupname">
...
</xs:group>
You must define an all, choice, or sequence element inside
the group declaration. The following example defines a
group named "persongroup", that defines a group of
elements that must occur in an exact sequence:
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
58
</xs:sequence>
59
Attribute Groups
Attribute groups are defined with the
attributeGroup declaration, like this:
<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>
The following example defines an attribute group
named "personattrgroup":
<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>
60
XSDThe<any>Element
The <any> element enables us to extend the XML
document with elements not specified by the schema.
The following example is a fragment from an XML
schema called "family.xsd". It shows a declaration for
the "person" element. By using the <any> element we
can extend (after <lastname>) the content of "person"
with any element:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
62
</xs:element>
63
64
XSDThe<anyAttribute>Element
The <anyAttribute> Element
The <anyAttribute> element enables us to extend the XML
document with attributes not specified by the schema.
The following example is a fragment from an XML schema
called "family.xsd". It shows a declaration for the "person"
element. By using the <anyAttribute> element we can add
any number of attributes to the "person" element:
<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:anyAttribute/>
</xs:complexType>
</xs:element>
66
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com attribute.xsd">
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person gender="male">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
The XML file above is valid because the schema "family.xsd"
allows us to add an attribute to the "person" element.
The <any> and <anyAttribute> elements are used to make
EXTENSIBLE documents! They allow documents to contain
additional elements that are not declared in the main XML 68
XSDElement Substitution
Let's say that we have users from two different countries:
England and Norway. We would like the ability to let the
user choose whether he or she would like to use the
Norwegian element names or the English element names
in the XML document.
To solve this problem, we could define
asubstitutionGroupin the XML schema. First, we
declare a head element and then we declare the other
elements which state that they are substitutable for the
head element.
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
In the example above, the "name" element is the head
element and the "navn" element is substitutable for
"name".
69
<xs:complexType name="custinfo">
<xs:sequence>
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
71
Using substitutionGroup
AnXSDExample
Let's have a look at this XML document called "shiporder.xml":
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
74
81
82
83
85
86
XSDStringDataTypes
StringDataType
Thestringdatatypecancontaincharacters,linefeeds,
carriagereturns,andtabcharacters.
Thefollowingisanexampleofastringdeclarationina
schema:
< xs:elem ent nam e= "custom er" type= "xs:string"/>
Anelementinyourdocumentmightlooklikethis:
< custom er> John Sm ith< /custom er>
Oritmightlooklikethis:
< custom er> John Sm ith < /custom er>
Note:TheXMLprocessorwillnotmodifythevalueifyou
87
usethestringdatatype.
NormalizedStringDataType
ThenormalizedStringdatatypeisderivedfromtheStringdatatype.
ThenormalizedStringdatatypealsocontainscharacters,butthe
XMLprocessorwillremovelinefeeds,carriagereturns,andtab
characters.
ThefollowingisanexampleofanormalizedStringdeclarationina
schema:
< xs:elem ent nam e= "custom er" type= "xs:norm alizedString"/>
Anelementinyourdocumentmightlooklikethis:
< custom er> John Sm ith< /custom er>
Oritmightlooklikethis:
< custom er> John Sm ith < /custom er>
Note:IntheexampleabovetheXMLprocessorwillreplacethe
tabswithspaces
88
TokenDataType
ThetokendatatypeisalsoderivedfromtheString
datatype.
Thetokendatatypealsocontainscharacters,butthe
XMLprocessorwillremovelinefeeds,carriagereturns,
tabs,leadingandtrailingspaces,andmultiplespaces.
Thefollowingisanexampleofatokendeclarationina
schema:
< xs:elem ent nam e= "custom er" type= "xs:token"/>
Anelementinyourdocumentmightlooklikethis:
< custom er> John Sm ith< /custom er>
Oritmightlooklikethis:
< custom er> John Sm ith < /custom er>
Note:IntheexampleabovetheXMLprocessorwill
removethetabs.
89
Description
ENTITIES
ENTITY
ID
AstringthatrepresentstheIDattributeinXML(onlyusedwithschemaattributes)
IDREF
AstringthatrepresentstheIDREFattributeinXML(onlyusedwithschemaattributes)
IDREFS
language
Astringthatcontainsavalidlanguageid
Name
AstringthatcontainsavalidXMLname
NCName
NMTOKEN
AstringthatrepresentstheNMTOKENattributeinXML(onlyusedwithschemaattributes)
NMTOKENS
normalizedString
Astringthatdoesnotcontainlinefeeds,carriagereturns,ortabs
QName
string
Astring
token
Astringthatdoesnotcontainlinefeeds,carriagereturns,tabs,leadingortrailingspaces,or
90
multiplespaces
TimeZones
Tospecifyatimezone,youcaneitherenteradateinUTCtimebyaddinga"Z"behind
thedate-likethis:
< start> 2002-09-24Z< /start>
oryoucanspecifyanoffsetfromtheUTCtimebyaddingapositiveornegativetime
behindthedate-likethis:
< start> 2002-09-24-06:00< /start>
or
< start> 2002-09-24+ 06:00< /start>
92
TimeDataType
Thetimedatatypeisusedtospecifya
time.
Thetimeisspecifiedinthefollowing
form"hh:mm:ss"where:
hhindicatesthehour
mmindicatestheminute
ssindicatesthesecond
Note:All components are required!
93
Description
date
Definesadatevalue
dateTime
Definesadateandtimevalue
duration
Definesatimeinterval
gDay
Definesapartofadate-theday(DD)
gMonth
Definesapartofadate-themonth(MM)
gMonthDay
Definesapartofadate-themonthandday(MM-DD)
gYear
Definesapartofadate-theyear(YYYY)
gYearMonth
Definesapartofadate-theyearandmonth(YYYYMM)
time
Definesatimevalue
100
101
XSDNumericData Types
DecimalDataType
Thedecimaldatatypeisusedtospecifyanumericvalue.
Thefollowingisanexampleofadecimaldeclarationinaschema:
< xs:elem ent nam e= "prize" type= "xs:decim al"/>
Anelementinyourdocumentmightlooklikethis:
< prize> 999.50< /prize>
Oritmightlooklikethis:
< prize> + 999.5450< /prize>
Oritmightlooklikethis:
< prize> -999.5230< /prize>
Oritmightlooklikethis:
< prize> 0< /prize>
Oritmightlooklikethis:
< prize> 14< /prize>
Note:Themaximumnumberofdecimaldigitsyoucanspecifyis18.
102
IntegerDataType
Theintegerdatatypeisusedtospecifyanumericvalue
withoutafractionalcomponent.
Thefollowingisanexampleofanintegerdeclarationina
schema:
< xs:elem ent nam e= "prize" type= "xs:integer"/>
Anelementinyourdocumentmightlooklikethis:
< prize> 999< /prize>
Oritmightlooklikethis:
< prize> + 999< /prize>
Oritmightlooklikethis:
< prize> -999< /prize>
Oritmightlooklikethis:
< prize> 0< /prize>
103
Description
byte
Asigned8-bitinteger
decimal
Adecimalvalue
int
Asigned32-bitinteger
integer
Anintegervalue
long
Asigned64-bitinteger
negativeInteger
Anintegercontainingonlynegativevalues(..,-2,-1)
nonNegativeInteger
Anintegercontainingonlynon-negativevalues(0,1,2,..)
nonPositiveInteger
Anintegercontainingonlynon-positivevalues(..,-2,-1,0)
positiveInteger
Anintegercontainingonlypositivevalues(1,2,..)
short
Asigned16-bitinteger
unsignedLong
Anunsigned64-bitinteger
unsignedInt
Anunsigned32-bitinteger
unsignedShort
Anunsigned16-bitinteger
unsignedByte
Anunsigned8-bitinteger
104
XSDMiscellaneousDataTypes
BooleanDataType
Thebooleandatatypeisusedtospecifyatrueorfalse
value.
Thefollowingisanexampleofabooleandeclarationina
schema:
< xs:attribute nam e= "disabled" type= "xs:boolean"/>
Anelementinyourdocumentmightlooklikethis:
< prize disabled= "true"> 999< /prize>
Note:Legalvaluesforbooleanaretrue,false,1(which
indicatestrue),and0(whichindicatesfalse).
106
BinaryDataTypes
Binarydatatypesareusedtoexpress
binary-formatteddata.
Wehavetwobinarydatatypes:
base64Binary(Base64-encodedbinarydata)
hexBinary(hexadecimal-encodedbinary
data)
Thefollowingisanexampleofa
hexBinarydeclarationinaschema:
< xs:elem ent nam e= "blobsrc"
type= "xs:hexBinary"/>
107
AnyURIDataType
TheanyURIdatatypeisusedtospecifyaURI.
ThefollowingisanexampleofananyURI
declarationinaschema:
< xs:attribute nam e= "src" type= "xs:anyU RI"/>
Anelementinyourdocumentmightlooklikethis:
< pic
src= "http://w w w .w 3schools.com /im ages/sm iley.gif" /
>
Note:IfaURIhasspaces,replacethemwith%20.
108
109
110
<?xmlversion="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:elementname="Employee"type="EmployeeType"/>
<xs:complexTypename="EmployeeType">
<xs:sequencemaxOccurs="unbounded">
<xs:elementref="Name"/>
<xs:elementref="Department"/>
</xs:sequence>
</xs:complexType>
<xs:elementname="Name"type="xs:string"/>
<xs:elementname="Department"type="xs:string"/>
</xs:schema>
A. <Employee></Employee>
B.<Employee>
<Name>MasashiTanaka</Name>
<Name>MakikoOkamura</Name>
</Employee>
C.<Employee>
<Name>MasashiTanaka</Name>
<Name>MakikoOkamura</Name>
<Department>SalesDepartment</Department>
<Department>AccountingDepartment</Department>
</Employee>
111
Oops!!
Finished!!
Thanks to
w3schools
112