XSD: XML Schema Language - PowerPoint PPT Presentation

About This Presentation
Title:

XSD: XML Schema Language

Description:

Title: Introduction to XML Author: Kanda Runapongsa Last modified by: Kanda Runapongsa Created Date: 11/6/2003 4:23:27 PM Document presentation format – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 49
Provided by: KandaRun
Category:

less

Transcript and Presenter's Notes

Title: XSD: XML Schema Language


1
XSD XML Schema Language
  • Kanda Runapongsa
  • (krunapon_at_kku.ac.th)
  • Dept. of Computer Engineering
  • Khon Kaen University

2
XML Schema DTD
  • Like a DTD, its purpose is to define the legal
    building blocks of an XML document
  • defines elements that can appear in a document
  • defines attributes that can appear in a document
  • defines which elements are child elements

3
XML Schema DTD (Cont.)
  • 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

4
Advantages of XSD over DTD
  • XML Schemas use XML syntax
  • Do not need to learn a new syntax
  • Use any XML parser to parse XML schemas
  • XML Schemas support many data types and allow you
    to create new data types
  • No Integer or Double types in DTD

5
Adv. of XSD over DTD (Cont.)
  • XML Schemas allow you to group elements to
    control the recurrence of elements and attributes
  • In DTD, we cannot specify that the number of
    occurrences of elements must be at least 2 and at
    most 5
  • XML Schemas support namespaces

6
The ltschemagt Element
  • The ltschemagt element is the root element of every
    XML Schema
  • lt?xml version"1.0"?gt
  • ltxsschemagt
  • ...
  • ...
  • lt/xsschemagt

7
The ltschemagt Element (Cont.)
  • lt?xml version"1.0"?gt
  • ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
    ema" targetNamespace"http//abc.com"
    xmlnshttp//abc.com" elementFormDefault"qualifi
    ed"gt
  • ...
  • ...
  • lt/xsschemagt

8
Attributes of ltSchemagt
  • xmlnsxshttp//www.w3.org/2001/XMLSchema
  • Indicates that the elements and data types used
    in the schema (schema, element,) come from the
    http//www.w3.org/2001/XMLSchema namespace
  • These elements and data types should be prefixed
    with xs

9
Attributes of ltSchemagt (Cont.)
  • targetNamespacehttp//abc.com
  • Indicates that the elements defined by this
    schema (Note, To, From,..) is in the
    "http//www.w3schools.com" namespace

10
Attributes of ltSchemagt (Cont.)
  • xmlnshttp//abc.com
  • Indicates that the default namespace is
    http//abc.com
  • elementFormDefault"qualified
  • indicates that any elements used by the XML
    instance document which were declared in this
    schema must be namespace qualified.

11
XSD Simple Elements
  • A simple element
  • Can contain only text
  • Cannot contain any other elements
  • Cannot contain any attributes
  • Can text be any other types besides string?
  • It can be date, etc., or even a custom type that
    you can define yourself

12
Defining a Simple Element
  • The syntax for defining a simple element is
  • ltxselement name"xxx" type"yyy"/gt
  • where xxx is the name of the element and yyy is
    the data type of the element.

13
Simple Elements Example
  • Some XML elements
  • ltlastnamegtWaseelt/lastnamegt
  • ltagegt34lt/agegt
  • Corresponding element definitions
  • ltxselement namelastname typexsstring/gt
  • ltxselement nameage typexsinteger/gt

14
Built-in Data Types
  • xsstring
  • xsdecimal
  • xsinteger
  • xsboolean
  • xsdate
  • xstime

15
Simple Elements Default Values
  • Simple elements can have a default value
  • A default value is automatically assigned to the
    element when no value is specified
  • ltxselement nameTo typexsstring
    defaultstudents/gt

16
Simple Elements Fixed Values
  • A fixed value is automatically assigned to
    elements
  • We cannot specify another value
  • ltxselement nameTo typexsstring
    fixedstudents/gt

17
XSD Attributes
  • All attributes are declared as simple types
  • Only complex elements can have attributes
  • An element with attributes always has a complex
    type definition

18
Defining an Attribute
  • Syntax
  • ltxsattribute namexxx typeyyy/gt
  • Where xxx is the name of the attribute and yyy is
    the data type of the attribute
  • Examples
  • XMLltlname langENgtDeelt/namegt
  • XSDltxsattribute namelang typexsstring/gt

19
Default Fixed Values for Attributes
  • Attributes can have a default value or a fixed
    value specified
  • Examples
  • ltxsattribute namelang typexsstring
    defaultEN/gt
  • ltxsattribute namelang typexsstring
    fixedEN/gt

20
Optional Required Attributes
  • All attributes are optional by default
  • To explicitly specify that the attribute is
    optional or required, use the use attribute
  • Examples
  • ltxsattribute namelang typexsstring
    useoptional/gt
  • ltxsattribute namelang typexsstring
    userequired/gt

21
Restrictions on Content
  • When an XML element or attribute has a type
    defined, it puts a restriction for the elements
    or attributes content
  • We can also add our own restrictions to our XML
    elements and attributes
  • These restrictions are called facets

22
XSD Restrictions/Facets
  • Restrictions are used to control acceptable
    values for XML elements or attributes
  • Many kinds of restrictions/facets
  • Restrictions on values
  • Restrictions on white spaces
  • Restrictions on length

23
Restrictions on Values
  • This example defines an element called age with
    a restriction.
  • ltxselement nameagegt
  • ltxssimpleTypegt
  • ltxsrestriction basexsintegergt
  • ltxsminInclusive value0/gt
  • ltxsmaxInclusive value60/gt
  • lt/xselementgt
  • What range the value of age is in?
  • Cannot be lower than 0 or greater than 60

24
Restriction on a Set of Values
  • To limit the content of an XML element to a set
    of acceptable values, we would use the
    enumeration constraint
  • ltxselement namegradegt
  • ltxssimpleTypegt
  • ltxsrestriction basexsstringgt
  • ltxsenumeration valueA/gt
  • ltxsenumeration valueB/gt
  • lt/xselementgt

25
Restrictions on a Series of Values
  • To limit the content to a series of numbers or
    letters, use the pattern constraint
  • ltxselement namelettergt
  • ltxssimpleTypegt
  • ltxsrestriction basexsstringgt
  • ltxspattern valuea-z/gt
  • lt/xselementgt
  • What are the acceptable values of the letter
    element?

26
Restrictions on a Series of Values
  • ltxselement name"letter"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt ltxspattern
    value"(a-zA-Z)"/gt
  • lt/xselementgt
  • What are the acceptable values of the letter
    element?

27
Restrictions on a Series of Values
  • ltxselement name"passwordgt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt ltxspattern
    value"a-zA-Z0-98"/gt
  • ltxselementgt
  • What are the acceptable values of the password
    element?

28
Restrictions on White Space Characters
  • To specify how white space characters should be
    handled, use the whiteSpace constraint
  • ltxselement name"address"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt
    ltxswhiteSpace value"preserve"/gt
  • lt/xselement
  • The XML processor will not remove any white space
    characters

29
Restrictions on White Space Characters
  • Three values of whiteSpace
  • preserve the XML processor will not remove any
    white space characters
  • replace the XML processor will replace all white
    space characters with spaces
  • collapse the XML processor will remove all white
    space characters (leading and trailing spaces are
    removed)

30
Restrictions on Length
  • ltxselement name"password"gt
  • ltxssimpleTypegt
  • ltxsrestriction base"xsstring"gt ltxslength
    value"8"/gt
  • lt/xselementgt
  • Can we specify the minimum and maximum length of
    the value?
  • Yes, use minLength and maxLength

31
What is a Complex Element?
  • A complex element is an XML element that 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

32
Complex Elements
  • Ex1 ltproduct pid1345/gt
  • Ex2 ltagtltbgt1lt/bgtltcgt2lt/cgtlt/agt
  • Ex3 ltfood typedessertgtIce creamlt/foodgt
  • Ex4 ltdescriptiongtToday is ltdategt11.13.2003lt/dategt
    lt/descriptiongt

33
Defining a Complex Element
  • ltxselement nameemployeegt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement namefirstname
    typexsstring/gt
  • ltxselement namelastname typexsstring/gt
  • lt/xselementgt

34
Sharing the Element Type
  • ltxselement nameemployee typepersoninfo/gt
  • ltxselement namestudent typepersoninfo/gt
  • ltxscomplexType namepersoninfogt
  • ltxssequencegt
  • ltxselement namefirstname
  • lt/xselementgt

35
Defining Based on Other Types
  • ltxselement name"employee" type"fullpersoninfo"/
    gt
  • ltxscomplexType name"personinfo"gt
  • lt/xscomplexTypegt
  • ltxscomplexType name"fullpersoninfo"gt
    ltxscomplexContentgt
  • ltxsextension base"personinfo"gt
  • ltxssequencegt
  • ltxselement name"address" type"xsstring
    "/gt
  • lt/xscomplexTypegt

36
Types for Empty Elements
  • XML ltproduct prodid"1345" /gt
  • XSD
  • ltxselement name"product" type"prodtype"/gt
  • ltxscomplexType name"prodtype"gt ltxsattribute
    name"prodid" type"xspositiveInteger"/gt
  • lt/xscomplexTypegt

37
Elements w/ Only Sub-Elements
  • XMLltpgtltfngtAlt/fngtltlngtBlt/lngtlt/pgt
  • XSD
  • ltxselement name"p" type"persontype"/gt
  • ltxscomplexType name"persontype"gt ltxssequencegt
  • ltxselement name"fn" type"xsstring"/gt
  • ltxselement name"ln" type"xsstring"/gt
  • lt/xssequencegt
  • lt/xscomplexTypegt

38
Elements w/ Only Text
  • XML ltshoesizegt8lt/shoesizegt
  • XSD
  • ltxselement name"shoesize" type"shoetype"/gt
  • ltxscomplexType name"shoetype"gt
    ltxssimpleContentgt
  • ltxsextension base"xsinteger"gt
  • lt/xscomplexTypegt

39
Types w/ Mixed Content
  • XML ltlettergtToday is ltdategt11.13.2003lt/dategtlt/let
    tergt
  • XSD
  • ltxselement name"letter"gt
  • ltxscomplexType mixed"true"gt
  • ltxssequencegt
  • ltxselement name"shipdate" type"xsdate"/gt
  • lt/xselementgt

40
Indicators
  • Order indicators
  • All, choice, sequence
  • Occurrence indicators
  • maxOccurs, minOccurs
  • Group indicators
  • Group name
  • attributeGroup name

41
All Indicator
  • Specify by default that the child elements can
    appear in any order and each child element must
    occur once and only once
  • ltxselement namepersongt
  • ltxscomplexTypegt
  • ltxsallgt
  • ltxselement namefname
  • typexsstring/gt
  • lt/xselementgt

42
Choice Indicator
  • Specify that either one child element or another
    can occur
  • ltxselement name"person"gt ltxscomplexTypegt
  • ltxschoicegt
  • ltxselement name"employee" type"employee"/
    gt
  • ltxselement name"member" type"member"/gt
  • ...
  • lt/xselementgt

43
Sequence Indicator
  • Specify that the child elements must appear in a
    specific order
  • ltxselement name"paper"gt ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement nameabstract" type"xsstring
    "/gt
  • ltxselement nameintroduction"
    type"xsstring"/gt
  • lt/xselementgt

44
Occurrence Indicator
  • Define how often an element can occur
  • maxOccurs Indicator maximum number of times an
    element can occur
  • ltxselement name"person"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement name"full_name"
    type"xsstring"/gt
  • ltxselement name"child_name"
    type"xsstring" maxOccurs"10"/gt
  • lt/xselementgt

45
Occurrence Indicator (Cont.)
  • minOccurs Indicator minimum number of times an
    element can occur
  • ltxselement name"person"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement name"full_name"
    type"xsstring"/gt
  • ltxselement name"child_name"
    type"xsstring" maxOccurs"10
    minOccurs0/gt
  • lt/xselementgt

46
Group Indicators
  • Define related sets of elements
  • Element Groups are defined with the group
    declaration, like this
  • ltxsgroup namegroupnamegt
  • lt/xsgroupgt
  • After defining a group, we can reference it in
    another complex type definition

47
Element Groups
  • ltxsgroup name"persongroup"gt
  • ltxssequencegt
  • ltxselement name"firstname" type"xsstring"/gt
  • lt/xsgroupgt
  • ltxscomplexType name"personinfo"gt ltxssequencegt
  • ltxsgroup ref"persongroup"/gt
  • ltxselement name"country" type"xsstring"/gt

48
Attribute Groups
  • ltxsattributeGroup name"personattrgroup"gt
    ltxsattribute name"firstname" type"xsstring"/gt
  • lt/xsattributeGroupgt
  • ltxselement name"person"gt
  • ltxscomplexTypegt
  • ltxsattributeGroup ref"personattrgroup"/gt
  • lt/xselementgt
Write a Comment
User Comments (0)
About PowerShow.com