Title: XML Training Course
1XML Training Course
- Module 4 XML Validation with XML Schemas
2What to expect this Session
- Shortcomings of DTDs
- What is an XML Schema?
- Discussion of Namespaces
- Defining Simple Elements
- Defining Attributes
- Defining Complex Elements
3DTDs Shortcoming
- They can only validate structure of a document.
- They can not enforce any type of rules for a
particular element. - To enforce rules use and XML Schema
4XML Schema
- Define the structure of an XML Document
- Constrain the content of the document
- Built using predefined XML elements and
attributes - Also called and XSD, XML Schema Definition
5Namespaces
- A way to package XML elements together for reuse.
- Provides a way to avoid element name conflicts
- ltPropertyDestroyed xmlnsCDshttp//www.foo.com/c
ds.xml - xmlnsArthttp//www.
oof.com/art.xmlgt - ltItem id23gt
- ltobjectgtMusic CDlt/objectgt
- ltCDsArtistgtNewsboyslt/CDsArtistgt
- ltCDsTitlegtThrivelt/CDsTitlegt
- lt/objectgt
- lt/Itemgt
- ltItem id24gt
- ltobjectgtPaintinglt/objectgt
- ltArtArtistgtPablo Picassolt/ArtArtistgt
- ltArtTitlegtDon Quixotelt/ArtTitlegt
- lt/objectgt
- lt/Itemgt
- lt/ProperyDestroyedgt
6Sample XSD
- lt?xml version"1.0"?gt
- ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema" targetNamespace"http//www.acs.cc.al.us" - xmlns"http//www.acs.cc.al.us"
elementFormDefault"qualified"gt - ltxselement nameMemo"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement nameTO" type"xsstring"/gt
- ltxselement nameCC" type"xsstring"/gt
- ltxselement nameFROM" type"xsstring"/gt
- ltxselement nameRE" type"xsstring"/gt
- ltxselement nameBODY" type"xsstring"/gt
- lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
- lt/xsschemagt
7ltschemagt
- The ltschemagt element is the root element of every
XML Schema - The ltschemagt element may contain some attributes.
- ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema" targetNamespace"http//www.acs.cc.al.us" - xmlns"http//www.acs.cc.al.us"
elementFormDefault"qualified"gt
8XSD - Simple Elements
- XML Schemas define the elements of your XML
files. - A simple element is an XML element that can
contain only text. It cannot contain any other
elements or attributes
9How to define a Simple Element
- Syntax
- ltxselement name"xxx" type"yyy"/gt
- Examples
10Common Data Types
- xsstring
- xsdecimal
- xsinteger
- xsboolean
- xsdate
- xstime
11Default and Fixed Values
- Simple elements can have a default value OR a
fixed value set - Default The value automatically assigned to the
element when no other value is specified - ltxselement name"color" type"xsstring"
default"red"/gt - Fixed A value automatically assigned to an
element. No other value may be specified. - ltxselement name"color" type"xsstring"
fixed"red"/gt
12XSD Attributes
- All attributes are declared as simple types.
- Only complex elements can have attributes!
13How to define an Attribute
- Synatax
- ltxsattribute name"xxx" type"yyy"/gt
- Example
14Default and Fixed Values
- Attributes, like simple elements, can have a
default value OR have a fixed value. - Default The value automatically assigned to the
attribute when no other value is specified - ltxsattribute namelang" type"xsstring"
defaultEN"/gt - Fixed A value automatically assigned to an
attribute. No other value may be specified. - ltxsattribute namelang" type"xsstring"
fixedEN"/gt
15Optional and Required Attributes
- All attributes are optional by default
- To explicitly specify that the attribute is
optional, use the "use" attribute - ltxsattribute name"lang" type"xsstring"
use"optional"/gt - To make an attribute required
- ltxsattribute name"lang" type"xsstring"
use"required"/gt
16XSD Restrictions and Facets
- Restrictions are used to control acceptable
values for XML elements or attributes. - Restrictions on XML elements are called facets.
17Restrictions on Values
- ltxselement name"age"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsinteger"gt
ltxsminInclusive value"0"/gt - ltxsmaxInclusive value"100"/gt
lt/xsrestrictiongt - lt/xssimpleTypegt
- lt/xselementgt
18Restrictions on a set of Values
- ltxselement name"car"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxsenumeration valueFord"/gt
ltxsenumeration valueDodge"/gt
ltxsenumeration valueSaturn"/gt
lt/xsrestrictiongt - lt/xssimpleTypegt
- lt/xselementgt
19Restrictions on a series of values
- ltxselement name"letter"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxspattern value"a-z"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
20Restrictions on a series of values
- ltxselement name"initials"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxspattern value"A-ZA-ZA-Z"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
21Restrictions on a series of values
- ltxselement name"initials"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxspattern value"a-zA-Za-zA-Za-zA-Z"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
22Restrictions on a series of values
- ltxselement name"choice"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxspattern value"xyz"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
23Restrictions on a series of values
- ltxselement name"prodid"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsinteger"gt
- ltxspattern value"0-90-90-90-90-9"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
24Other uses for the Pattern Constraint
- ltxselement name"gender"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxspattern value"malefemale"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
25Restrictions on White Space
- ltxselement name"address"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxswhiteSpace value"preserve"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
26Restrictions on White Space
- ltxselement name"address"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxswhiteSpace value"replace"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
27Restrictions on White Space
- ltxselement name"address"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxswhiteSpace value"collapse"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
28Restrictions on Length
- ltxselement name"password"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxslength value"8"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
29Restrictions on Length
- ltxselement name"password"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxsminLength value"5"/gt
- ltxsmaxLength value"8"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
30DataType Restriction options
31XSD - Complex Elements
- A complex element contains other elements and/or
attributes. - Four 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 - Each of these element may contain attributes as
well.
32Complex Element Examples
- Empty Element
- ltproduct pid"1345"/gt
- Element contains only other elements
- ltemployeegt
- ltfirstnamegtJohnlt/firstnamegt
- ltlastnamegtSmithlt/lastnamegt
- lt/employeegt
33Complex Element Examples
- Element that contains only text
- ltfood type"dessert"gtIce creamlt/foodgt
- Element that is MIXED, that is, contains both
elements and text - ltdescriptiongt It happened on
- ltdate lang"norwegian"gt03.03.99lt/dategt ....
lt/descriptiongt
34How to define a Complex ElementMethod 1
- Complex Element that contains only other
elements - ltemployeegt
- ltfirstnamegtJohnlt/firstnamegt
- ltlastnamegtSmithlt/lastnamegt
- lt/employeegt
- XML Schema
- ltxselement name"employee"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name"firstname"
type"xsstring"/gt - ltxselement name"lastname"
type"xsstring"/gt - lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
35How to define a Complex ElementMethod 2
- ltxselement name"employee" type"personinfo"/gt
- ltxscomplexType name"personinfo"gt
- ltxssequencegt
- ltxselement name"firstname"
type"xsstring"/gt - ltxselement name"lastname" type"xsstring"/gt
- lt/xssequencegt
- lt/xscomplexTypegt
36How to define a Complex ElementMethod 2
- ltxselement name"employee" type"personinfo"/gt
- ltxselement name"student" type"personinfo"/gt
- ltxselement name"member" type"personinfo"/gt
- ltxscomplexType name"personinfo"gt
- ltxssequencegt
- ltxselement name"firstname" type"xsstring"/gt
- ltxselement name"lastname" type"xsstring"/gt
- lt/xssequencegt
- lt/xscomplexTypegt
37Define an EMPTY Complex Element
- An empty complex element can contain attributes
but it cannot have any content between the
opening and closing tags. - ltproduct prodid"1345" /gt
38Define an EMPTY Complex Element
- ltxselement name"product"gt
- ltxscomplexTypegt
- ltxscomplexContentgt
- ltxsrestriction base"xsinteger"gt
- ltxsattribute name"prodid
type"xspositiveInteger"/gt - lt/xsrestrictiongt
- lt/xscomplexContentgt
- lt/xscomplexTypegt
- lt/xselementgt
39Define a TEXT-ONLY Complex Element
- A complex text element that contains both
attributes and text, but not other 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
40Extension Syntax
- ltxselement name"somename"gt
- ltxscomplexTypegt
- ltxssimpleContentgt
- ltxsextension base"basetype"gt
- ....
- ....
- lt/xsextensiongt
- lt/xssimpleContentgt
- lt/xscomplexTypegt
- lt/xselementgt
41Extension Example
- Element
- ltshoesize country"france"gt35lt/shoesizegt
- Schema Definition
- ltxselement name"shoesize"gt
- ltxscomplexTypegt
- ltxssimpleContentgt
- ltxsextension base"xsinteger"gt
- ltxsattribute name"country" type"xsstring"
/gt - lt/xsextensiongt
- lt/xssimpleContentgt
- lt/xscomplexTypegt
- lt/xselementgt
42Define a MIXED Complex Element
- A mixed complex type element can contain
attributes, elements, and text. - ltlettergt
- Dear Mr.ltnamegtJohn Smithlt/namegt. Your order
ltorderidgt1032lt/orderidgt will be shipped on
ltshipdategt2001-07-13lt/shipdategt. - lt/lettergt
43Define a MIXED Complex Element
- ltxselement name"letter"gt
- ltxscomplexType mixed"true"gt
- ltxssequencegt
- ltxselement name"name" type"xsstring"/gt
- ltxselement name"orderid" type"xspositiveInt
eger"/gt - ltxselement name"shipdate" type"xsdate"/gt
- lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
44Review this Session
- Shortcomings of DTDs
- What is an XML Schema?
- Discussion of Namespaces
- Defining Simple Elements
- Defining Attributes
- Defining Complex Elements
45Build an XML Schema