XML Schema - PowerPoint PPT Presentation

About This Presentation
Title:

XML Schema

Description:

XML Schema Andy Clark What is it? A grammar definition language Like DTDs but better Uses XML syntax Defined by W3C Primary features Datatypes e.g. integer, float ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 50
Provided by: AndyC69
Learn more at: http://people.apache.org
Category:
Tags: xml | grammar | lexical | schema

less

Transcript and Presenter's Notes

Title: XML Schema


1
XML Schema
  • Andy Clark

2
What is it?
  • A grammar definition language
  • Like DTDs but better
  • Uses XML syntax
  • Defined by W3C
  • Primary features
  • Datatypes
  • e.g. integer, float, date, etc
  • More powerful content models
  • e.g. namespace-aware, type derivation, etc

3
XML Schema Types
  • Simple types
  • Basic datatypes
  • Can be used for attributes and element text
  • Extendable
  • Complex types
  • Defines structure of elements
  • Extendable
  • Types can be named or anonymous

4
Simple Types
  • DTD datatypes
  • Strings, ID/IDREF, NMTOKEN, etc
  • Numbers
  • Integer, long, float, double, etc
  • Other
  • Binary (base64, hex)
  • QName, URI, date/time
  • etc

5
Deriving Simple Types
  • Apply facets
  • Specify enumerated values
  • Add restrictions to data
  • Restrict lexical space
  • Allowed length, pattern, etc
  • Restrict value space
  • Minimum/maximum values, etc
  • Extend by list or union

6
A Simple Type Example (1 of 4)
  • Integer with value (1234, 5678

01 ltxsdsimpleType nameMyIntegergt 02
ltxsdrestriction basexsdintegergt 03
ltxsdminExclusive value1234/gt 04
ltxsdmaxInclusive value5678/gt 05
lt/xsdrestrictiongt 06 lt/xsdsimpleTypegt
7
A Simple Type Example (2 of 4)
  • Integer with value (1234, 5678

01 ltxsdsimpleType nameMyIntegergt 02
ltxsdrestriction basexsdintegergt 03
ltxsdminExclusive value1234/gt 04
ltxsdmaxInclusive value5678/gt 05
lt/xsdrestrictiongt 06 lt/xsdsimpleTypegt
8
A Simple Type Example (3 of 4)
  • Integer with value (1234, 5678

01 ltxsdsimpleType nameMyIntegergt 02
ltxsdrestriction basexsdintegergt 03
ltxsdminExclusive value1234/gt 04
ltxsdmaxInclusive value5678/gt 05
lt/xsdrestrictiongt 06 lt/xsdsimpleTypegt
9
A Simple Type Example (4 of 4)
  • Validating integer with value (1234, 5678

01 ltdata xsitype'MyInteger'gtlt/datagt INVALID
02 ltdata xsitype'MyInteger'gtAndylt/datagt INVA
LID 03 ltdata xsitype'MyInteger'gt-32lt/datagt I
NVALID 04 ltdata xsitype'MyInteger'gt1233lt/data
gt INVALID 05 ltdata xsitype'MyInteger'gt1234lt/
datagt INVALID 06 ltdata xsitype'MyInteger'gt12
35lt/datagt 07 ltdata xsitype'MyInteger'gt5678lt/d
atagt 08 ltdata xsitype'MyInteger'gt5679lt/datagt
INVALID
10
Complex Types
  • Element content models
  • Simple
  • Mixed
  • Unlike DTDs, elements in mixed content can be
    ordered
  • Sequences and choices
  • Can contain nested sequences and choices
  • All
  • All elements required but order is not important

11
A Complex Type Example (1 of 5)
  • Mixed content that allows ltbgt, ltigt, and ltugt

01 ltxsdcomplexType nameRichText
mixedtruegt 02 ltxsdchoice minOccurs0
maxOccursunboundedgt 03 ltxsdelement
nameb typeRichText/gt 04 ltxsdelement
namei typeRichText/gt 05 ltxsdelement
nameu typeRichText/gt 06
lt/xsdchoicegt 07 lt/xsdcomplexTypegt
12
A Complex Type Example (2 of 5)
  • Mixed content that allows ltbgt, ltigt, and ltugt

01 ltxsdcomplexType nameRichText
mixedtruegt 02 ltxsdchoice minOccurs0
maxOccursunboundedgt 03 ltxsdelement
nameb typeRichText/gt 04 ltxsdelement
namei typeRichText/gt 05 ltxsdelement
nameu typeRichText/gt 06
lt/xsdchoicegt 07 lt/xsdcomplexTypegt
13
A Complex Type Example (3 of 5)
  • Mixed content that allows ltbgt, ltigt, and ltugt

01 ltxsdcomplexType nameRichText
mixedtruegt 02 ltxsdchoice minOccurs0
maxOccursunboundedgt 03 ltxsdelement
nameb typeRichText/gt 04 ltxsdelement
namei typeRichText/gt 05 ltxsdelement
nameu typeRichText/gt 06
lt/xsdchoicegt 07 lt/xsdcomplexTypegt
14
A Complex Type Example (4 of 5)
  • Mixed content that allows ltbgt, ltigt, and ltugt

01 ltxsdcomplexType nameRichText
mixedtruegt 02 ltxsdchoice minOccurs0
maxOccursunboundedgt 03 ltxsdelement
nameb typeRichText/gt 04 ltxsdelement
namei typeRichText/gt 05 ltxsdelement
nameu typeRichText/gt 06
lt/xsdchoicegt 07 lt/xsdcomplexTypegt
15
A Complex Type Example (5 of 5)
  • Validation of RichText

01 ltcontent xsitype'RichText'gtlt/contentgt 02
ltcontent xsitype'RichText'gtAndylt/contentgt 03
ltcontent xsitype'RichText'gtXML is
ltigtawesomelt/igt.lt/contentgt 04 ltcontent
xsitype'RichText'gtltBgtboldlt/Bgtlt/contentgt INVALID
05 ltcontent xsitype'RichText'gtltfoo/gtlt/content
gt INVALID
16
Flexing Our Muscles
  • The task
  • Converting a DTD grammar to XML Schema
  • Defining datatypes
  • Beyond what DTDs allow
  • More precise control over string values
  • Defining content models

17
Converting DTD (1 of 27)
  • Original DTD grammar

01 lt!ELEMENT order (item) gt 02 03 lt!ELEMENT ite
m (name,price) gt 04 lt!ATTLIST item code NMTOKEN
REQUIRED gt 05 06 lt!ELEMENT name (PCDATA)
gt 07 08 lt!ELEMENT price (PCDATA)
gt 09 lt!ATTLIST price currency NMTOKEN 'USD gt
18
Converting DTD (2 of 27)
  • Original DTD grammar

01 lt!ELEMENT order (item) gt 02 03 lt!ELEMENT ite
m (name,price) gt 04 lt!ATTLIST item code NMTOKEN
REQUIRED gt 05 06 lt!ELEMENT name (PCDATA)
gt 07 08 lt!ELEMENT price (PCDATA)
gt 09 lt!ATTLIST price currency NMTOKEN 'USD gt
19
Converting DTD (3 of 27)
  • Original DTD grammar

01 lt!ELEMENT order (item) gt 02 03 lt!ELEMENT ite
m (name,price) gt 04 lt!ATTLIST item code NMTOKEN
REQUIRED gt 05 06 lt!ELEMENT name (PCDATA)
gt 07 08 lt!ELEMENT price (PCDATA)
gt 09 lt!ATTLIST price currency NMTOKEN 'USD gt
20
Converting DTD (4 of 27)
  • Create XML Schema document
  • Grammar with no target namespace

01 ltxsdschema xmlnsxsdhttp//www.w3.org/2001/
XMLSchemagt nn lt/xsdschemagt
21
Converting DTD (5 of 27)
  • Create XML Schema document
  • Grammar with target namespace
  • Note Its important to bind the namespace to a
    prefix because references to names within the XML
    Schema must be fully qualified.

01 ltxsdschema xmlnsxsdhttp//www.w3.org/2001/
XMLSchema 02 xmlnsaNS 03 targetNamespace
NSgt nn lt/xsdschemagt
22
Converting DTD (6 of 27)
  • Declare elements

01 ltxsdschema xmlnsxsd'http//www.w3.org/2001/
XMLSchema'gt 02 03 ltxsdelement nameorder ty
pe'Order'/gt 04 ltxsdelement name'item type'I
tem'/gt 05 ltxsdelement name'name type'Name'/gt
06 ltxsdelement name'price type'Price'/gt
23
Converting DTD (7 of 27)
  • Declare elements
  • Note In general, its a good idea to reference
    named types and avoid anonymous (or inline)
    types. Beware of qualified form option.

01 ltxsdschema xmlnsxsd'http//www.w3.org/2001/
XMLSchema'gt 02 03 ltxsdelement nameorder ty
pe'Order'/gt 04 ltxsdelement name'item type'I
tem'/gt 05 ltxsdelement name'name type'Name'/gt
06 ltxsdelement name'price type'Price'/gt
24
Converting DTD (8 of 27)
  • Define type for ltordergt element

08 lt!-- lt!ELEMENT item (item)gt --gt 09
ltxsdcomplexType name'Order'gt 10
ltxsdsequencegt 11 ltxsdelement ref'item'
minOccurs'1' maxOccurs'unbounded'/gt 12
lt/xsdsequencegt 13 lt/xsdcomplexTypegt
25
Converting DTD (9 of 27)
  • Define type for ltordergt element

08 lt!-- lt!ELEMENT item (item)gt --gt 09
ltxsdcomplexType name'Order'gt 10
ltxsdsequencegt 11 ltxsdelement ref'item'
minOccurs'1' maxOccurs'unbounded'/gt 12
lt/xsdsequencegt 13 lt/xsdcomplexTypegt
26
Converting DTD (10 of 27)
  • Define type for ltordergt element

08 lt!-- lt!ELEMENT item (item)gt --gt 09
ltxsdcomplexType name'Order'gt 10
ltxsdsequencegt 11 ltxsdelement ref'item'
minOccurs'1' maxOccurs'unbounded'/gt 12
lt/xsdsequencegt 13 lt/xsdcomplexTypegt
27
Converting DTD (11 of 27)
  • Define type for ltitemgt element

15 lt!-- lt!ELEMENT item (name,price)gt --gt 16
ltxsdcomplexType name'Item'gt 17
ltxsdsequencegt 18 ltxsdelement
ref'name'/gt 19 ltxsdelement
ref'price'/gt 20 lt/xsdsequencegt 21
lt!-- lt!ATTLIST item code NMTOKEN REQUIREDgt
--gt 22 ltxsdattribute name'code'gt 23
ltxsdsimpleTypegt 24 ltxsdrestriction
base'xsdstring'gt 25 ltxsdpattern
value'A-Z2\d3'/gt 26
lt/xsdrestrictiongt 27 lt/xsdsimpleTypegt 28
lt/xsdattributegt 29 lt/xsdcomplexTypegt
28
Converting DTD (12 of 27)
  • Define type for ltitemgt element

15 lt!-- lt!ELEMENT item (name,price)gt --gt 16
ltxsdcomplexType name'Item'gt 17
ltxsdsequencegt 18 ltxsdelement
ref'name'/gt 19 ltxsdelement
ref'price'/gt 20 lt/xsdsequencegt 21
lt!-- lt!ATTLIST item code NMTOKEN REQUIREDgt
--gt 22 ltxsdattribute name'code'gt 23
ltxsdsimpleTypegt 24 ltxsdrestriction
base'xsdstring'gt 25 ltxsdpattern
value'A-Z2\d3'/gt 26
lt/xsdrestrictiongt 27 lt/xsdsimpleTypegt 28
lt/xsdattributegt 29 lt/xsdcomplexTypegt
29
Converting DTD (13 of 27)
  • Define type for ltitemgt element

15 lt!-- lt!ELEMENT item (name,price)gt --gt 16
ltxsdcomplexType name'Item'gt 17
ltxsdsequencegt 18 ltxsdelement
ref'name'/gt 19 ltxsdelement
ref'price'/gt 20 lt/xsdsequencegt 21
lt!-- lt!ATTLIST item code NMTOKEN REQUIREDgt
--gt 22 ltxsdattribute name'code'gt 23
ltxsdsimpleTypegt 24 ltxsdrestriction
base'xsdstring'gt 25 ltxsdpattern
value'A-Z2\d3'/gt 26
lt/xsdrestrictiongt 27 lt/xsdsimpleTypegt 28
lt/xsdattributegt 29 lt/xsdcomplexTypegt
30
Converting DTD (14 of 27)
  • Define type for ltitemgt element

15 lt!-- lt!ELEMENT item (name,price)gt --gt 16
ltxsdcomplexType name'Item'gt 17
ltxsdsequencegt 18 ltxsdelement
ref'name'/gt 19 ltxsdelement
ref'price'/gt 20 lt/xsdsequencegt 21
lt!-- lt!ATTLIST item code NMTOKEN REQUIREDgt
--gt 22 ltxsdattribute name'code'gt 23
ltxsdsimpleTypegt 24 ltxsdrestriction
base'xsdstring'gt 25 ltxsdpattern
value'A-Z2\d3'/gt 26
lt/xsdrestrictiongt 27 lt/xsdsimpleTypegt 28
lt/xsdattributegt 29 lt/xsdcomplexTypegt
31
Converting DTD (15 of 27)
  • Define type for ltitemgt element

15 lt!-- lt!ELEMENT item (name,price)gt --gt 16
ltxsdcomplexType name'Item'gt 17
ltxsdsequencegt 18 ltxsdelement
ref'name'/gt 19 ltxsdelement
ref'price'/gt 20 lt/xsdsequencegt 21
lt!-- lt!ATTLIST item code NMTOKEN REQUIREDgt
--gt 22 ltxsdattribute name'code'gt 23
ltxsdsimpleTypegt 24 ltxsdrestriction
base'xsdstring'gt 25 ltxsdpattern
value'A-Z2\d3'/gt 26
lt/xsdrestrictiongt 27 lt/xsdsimpleTypegt 28
lt/xsdattributegt 29 lt/xsdcomplexTypegt
32
Converting DTD (16 of 27)
  • Define type for ltnamegt element

31 lt!-- lt!ELEMENT name (PCDATA)gt --gt 32
ltxsdsimpleType name'Name'gt 33
ltxsdrestriction base'xsdstring'/gt 34
lt/xsdsimpleTypegt
33
Converting DTD (17 of 27)
  • Define type for ltnamegt element

31 lt!-- lt!ELEMENT name (PCDATA)gt --gt 32
ltxsdsimpleType name'Name'gt 33
ltxsdrestriction base'xsdstring'/gt 34
lt/xsdsimpleTypegt
34
Converting DTD (18 of 27)
  • Define type for ltnamegt element

31 lt!-- lt!ELEMENT name (PCDATA)gt --gt 32
ltxsdsimpleType name'Name'gt 33
ltxsdrestriction base'xsdstring'/gt 34
lt/xsdsimpleTypegt
35
Converting DTD (19 of 27)
  • Define type for ltpricegt element

36 lt!-- lt!ELEMENT price (PCDATA)gt --gt 37
ltxsdcomplexType name'Price'gt 38
ltxsdsimpleContentgt 39 ltxsdextension
base'NonNegativeDouble'gt 40 lt!--
lt!ATTLIST price currency NMTOKEN 'USD'gt --gt 41
ltxsdattribute name'currency'
default'USD'gt 42 ltxsdsimpleTypegt 43
ltxsdrestriction
base'xsdstring'gt 44
ltxsdpattern value'A-Z3'/gt 45
lt/xsdrestrictiongt 46
lt/xsdsimpleTypegt 47 lt/xsdattributegt 48
lt/xsdextensiongt 49
lt/xsdsimpleContentgt 50 lt/xsdcomplexTypegt
36
Converting DTD (20 of 27)
  • Define type for ltpricegt element

36 lt!-- lt!ELEMENT price (PCDATA)gt --gt 37
ltxsdcomplexType name'Price'gt 38
ltxsdsimpleContentgt 39 ltxsdextension
base'NonNegativeDouble'gt 40 lt!--
lt!ATTLIST price currency NMTOKEN 'USD'gt --gt 41
ltxsdattribute name'currency'
default'USD'gt 42 ltxsdsimpleTypegt 43
ltxsdrestriction
base'xsdstring'gt 44
ltxsdpattern value'A-Z3'/gt 45
lt/xsdrestrictiongt 46
lt/xsdsimpleTypegt 47 lt/xsdattributegt 48
lt/xsdextensiongt 49
lt/xsdsimpleContentgt 50 lt/xsdcomplexTypegt
37
Converting DTD (21 of 27)
  • Define type for ltpricegt element

36 lt!-- lt!ELEMENT price (PCDATA)gt --gt 37
ltxsdcomplexType name'Price'gt 38
ltxsdsimpleContentgt 39 ltxsdextension
base'NonNegativeDouble'gt 40 lt!--
lt!ATTLIST price currency NMTOKEN 'USD'gt --gt 41
ltxsdattribute name'currency'
default'USD'gt 42 ltxsdsimpleTypegt 43
ltxsdrestriction
base'xsdstring'gt 44
ltxsdpattern value'A-Z3'/gt 45
lt/xsdrestrictiongt 46
lt/xsdsimpleTypegt 47 lt/xsdattributegt 48
lt/xsdextensiongt 49
lt/xsdsimpleContentgt 50 lt/xsdcomplexTypegt
38
Converting DTD (22 of 27)
  • Define type for ltpricegt element

36 lt!-- lt!ELEMENT price (PCDATA)gt --gt 37
ltxsdcomplexType name'Price'gt 38
ltxsdsimpleContentgt 39 ltxsdextension
base'NonNegativeDouble'gt 40 lt!--
lt!ATTLIST price currency NMTOKEN 'USD'gt --gt 41
ltxsdattribute name'currency'
default'USD'gt 42 ltxsdsimpleTypegt 43
ltxsdrestriction
base'xsdstring'gt 44
ltxsdpattern value'A-Z3'/gt 45
lt/xsdrestrictiongt 46
lt/xsdsimpleTypegt 47 lt/xsdattributegt 48
lt/xsdextensiongt 49
lt/xsdsimpleContentgt 50 lt/xsdcomplexTypegt
39
Converting DTD (23 of 27)
  • Define type for ltpricegt element

36 lt!-- lt!ELEMENT price (PCDATA)gt --gt 37
ltxsdcomplexType name'Price'gt 38
ltxsdsimpleContentgt 39 ltxsdextension
base'NonNegativeDouble'gt 40 lt!--
lt!ATTLIST price currency NMTOKEN 'USD'gt --gt 41
ltxsdattribute name'currency'
default'USD'gt 42 ltxsdsimpleTypegt 43
ltxsdrestriction
base'xsdstring'gt 44
ltxsdpattern value'A-Z3'/gt 45
lt/xsdrestrictiongt 46
lt/xsdsimpleTypegt 47 lt/xsdattributegt 48
lt/xsdextensiongt 49
lt/xsdsimpleContentgt 50 lt/xsdcomplexTypegt
40
Converting DTD (24 of 27)
  • Define simple type for use with Price type

52 ltxsdsimpleType name'NonNegativeDouble'gt 5
3 ltxsdrestriction base'xsddouble'gt 54
ltxsdminInclusive value'0.00'/gt 55
lt/xsdrestrictiongt 56 lt/xsdsimpleTypegt 57 58
lt/xsdschemagt
41
Converting DTD (25 of 27)
  • Define simple type for use with Price type

52 ltxsdsimpleType name'NonNegativeDouble'gt 5
3 ltxsdrestriction base'xsddouble'gt 54
ltxsdminInclusive value'0.00'/gt 55
lt/xsdrestrictiongt 56 lt/xsdsimpleTypegt 57 58
lt/xsdschemagt
42
Converting DTD (26 of 27)
  • Define simple type for use with Price type

52 ltxsdsimpleType name'NonNegativeDouble'gt 5
3 ltxsdrestriction base'xsddouble'gt 54
ltxsdminInclusive value'0.00'/gt 55
lt/xsdrestrictiongt 56 lt/xsdsimpleTypegt 57 58
lt/xsdschemagt
43
Converting DTD (27 of 27)
  • Size comparison

Note the verbosity of the XML Schema version of
the grammar. (Im using a 4 pt. font!)
44
Using XML Schema Grammar to validate Document
  • Bind XML Schema instance namespace
  • e.g. xmlnsxsihttp//www.w3.org/2001/XMLSchema-i
    nstance
  • Grammar with no target namespace
  • e.g. xsinoNamespaceSchemaLocationgrammar.xsd
  • Grammar with target namespace
  • Namespace URI and systemId pairs
  • e.g. xsischemaLocationNS grammar.xsd

45
Example Document (1 of 3)
  • Grammar with no target namespace

01 lt?xml version1.0 encodingShift_JIS?gt 02
ltorder xmlnsxsihttp//wwww.w3.org/2001/XMLSche
ma-instance 03 xsinoNamespaceSchemaLoca
tiongrammar.xsdgt 04 ltitem codeBK123gt 05
ltnamegt??????????lt/namegt 06 ltprice
currencyJPNgt5460lt/pricegt 07 lt/itemgt 08
lt/ordergt
46
Example Document (2 of 3)
  • Grammar with target namespace (1 of 2)

01 lt?xml version1.0 encodingShift_JIS?gt 02
ltorder xmlnsxsihttp//wwww.w3.org/2001/XMLSc
hema-instance 03 xsischemaLocationNS g
rammar.xsd 04 xmlnsNSgt 05 ltitem
codeBK123gt 06 ltnamegt??????????lt/namegt 07
ltprice currencyJPNgt5460lt/pricegt 08
lt/itemgt 09 lt/ordergt
47
Example Document (3 of 3)
  • Grammar with target namespace (2 of 2)

01 lt?xml version1.0 encodingShift_JIS?gt 02
ltorder xmlnsxsihttp//wwww.w3.org/2001/XMLSc
hema-instance 03 xsischemaLocationNS g
rammar.xsd 04 xmlnsNSgt 05 ltitem
codeBK123gt 06 ltnamegt??????????lt/namegt 07
ltprice currencyJPNgt5460lt/pricegt 08
lt/itemgt 09 lt/ordergt
48
Useful Links
  • XML Schema Specification
  • Part 0 http//www.w3.org/TR/xmlschema-0/
  • Part 1 http//www.w3.org/TR/xmlschema-1/
  • Part 2 http//www.w3.org/TR/xmlschema-2/
  • XML.com articles
  • http//www.xml.com/pub/a/2001/06/06/schemasimple.h
    tml

49
XML Schema
  • Andy Clark
Write a Comment
User Comments (0)
About PowerShow.com