XML Training Course - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

XML Training Course

Description:

Restrictions are used to control acceptable values for XML elements or attributes. ... Restrictions on a set of Values xs:element name='car' xs:simpleType ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 46
Provided by: Bowm
Category:
Tags: xml | course | training

less

Transcript and Presenter's Notes

Title: XML Training Course


1
XML Training Course
  • Module 4 XML Validation with XML Schemas

2
What to expect this Session
  • Shortcomings of DTDs
  • What is an XML Schema?
  • Discussion of Namespaces
  • Defining Simple Elements
  • Defining Attributes
  • Defining Complex Elements

3
DTDs 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

4
XML 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

5
Namespaces
  • 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

6
Sample 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

7
ltschemagt
  • 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

8
XSD - 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

9
How to define a Simple Element
  • Syntax
  • ltxselement name"xxx" type"yyy"/gt
  • Examples

10
Common Data Types
  • xsstring
  • xsdecimal
  • xsinteger
  • xsboolean
  • xsdate
  • xstime

11
Default 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

12
XSD Attributes
  • All attributes are declared as simple types.
  • Only complex elements can have attributes!

13
How to define an Attribute
  • Synatax
  • ltxsattribute name"xxx" type"yyy"/gt
  • Example

14
Default 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

15
Optional 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

16
XSD Restrictions and Facets
  • Restrictions are used to control acceptable
    values for XML elements or attributes.
  • Restrictions on XML elements are called facets.

17
Restrictions 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

18
Restrictions 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

19
Restrictions 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

20
Restrictions 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

21
Restrictions 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

22
Restrictions on a series of values
  • ltxselement name"choice"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt
  • ltxspattern value"xyz"/gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt
  • lt/xselementgt

23
Restrictions 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

24
Other uses for the Pattern Constraint
  • ltxselement name"gender"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt
  • ltxspattern value"malefemale"/gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt
  • lt/xselementgt

25
Restrictions on White Space
  • ltxselement name"address"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt
  • ltxswhiteSpace value"preserve"/gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt
  • lt/xselementgt

26
Restrictions on White Space
  • ltxselement name"address"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt
  • ltxswhiteSpace value"replace"/gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt
  • lt/xselementgt

27
Restrictions on White Space
  • ltxselement name"address"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt
  • ltxswhiteSpace value"collapse"/gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt
  • lt/xselementgt

28
Restrictions on Length
  • ltxselement name"password"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt
  • ltxslength value"8"/gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt
  • lt/xselementgt

29
Restrictions 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

30
DataType Restriction options
31
XSD - 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.

32
Complex Element Examples
  • Empty Element
  • ltproduct pid"1345"/gt
  • Element contains only other elements
  • ltemployeegt
  • ltfirstnamegtJohnlt/firstnamegt
  • ltlastnamegtSmithlt/lastnamegt
  • lt/employeegt

33
Complex 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

34
How 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

35
How 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

36
How 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

37
Define 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

38
Define 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

39
Define 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

40
Extension Syntax
  • ltxselement name"somename"gt
  • ltxscomplexTypegt
  • ltxssimpleContentgt
  • ltxsextension base"basetype"gt
  • ....
  • ....
  • lt/xsextensiongt
  • lt/xssimpleContentgt
  • lt/xscomplexTypegt
  • lt/xselementgt

41
Extension 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

42
Define 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

43
Define 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

44
Review this Session
  • Shortcomings of DTDs
  • What is an XML Schema?
  • Discussion of Namespaces
  • Defining Simple Elements
  • Defining Attributes
  • Defining Complex Elements

45
Build an XML Schema
Write a Comment
User Comments (0)
About PowerShow.com