XML Schema - PowerPoint PPT Presentation

About This Presentation
Title:

XML Schema

Description:

XML Schema The main source for these s is The XML Companion by Bradley Other resources: http://www.w3.org/TR/xmlschema-2/ XML Schema XSDL Alternatives ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 65
Provided by: mm6
Category:
Tags: xml | relax | schema

less

Transcript and Presenter's Notes

Title: XML Schema


1
XML Schema
The main source for these slides is The XML
Companion by Bradley Other resources
http//www.w3.org/TR/xmlschema-2/
2
XML Schema
  • XML Schema is the official name
  • XSDL (XML Schema Definition Language) is the
    language
  • used to create schema definitions
  • Can be used to more tightly constrain a document
    instance
  • Supports namespaces
  • Permits type derivation

3
XSDL Alternatives Include
  • DTDs
  • RELAX
  • TREX (James Clark - Tree Regular Expressions for
    XML)
  • RELAX NG (RELAX and TREX combined to Relax Next
    Generation)
  • Schematron (Rule based rather than grammar
    based see www.ascc.net/xml/schematron) Based on
    XSLT and XPath

4
XSDL - A Simple Purchase Order
lt?xml version"1.0" encoding"UTF-8"?gt lt!--
po.xml --gt ltpurchaseOrder orderDate"07.23.2001"
xmlns"http//www.cds-r-us.com"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" xsischemaLocation"http//www.cds-r-us.
com po.xsd" gt
5
ltrecipient country"USA"gt ltnamegtDennis
Scannellt/namegt ltstreetgt175 Perry Lea
Side Roadlt/streetgt ltcitygtWaterburylt/citygt
ltstategtVTlt/stategt
ltpostalCodegt15216lt/postalCodegt lt/recipientgt
ltordergt ltcd artist"Brooks Williams"
title"Little Lion" /gt ltcd artist"David
Wilcox" title"What you whispered" /gt
lt/ordergt lt/purchaseOrdergt
6
Purchase Order XSDL
lt?xml version"1.0" encoding"utf-8"?gt lt!--
po.xsd --gt ltxsschema xmlnsxs"http//www.w3.
org/2001/XMLSchema" xmlns"http//www.cds-r-us.
com" targetNamespace"http//www.cds-r-us.com"
gt
7
ltxselement name"purchaseOrder"gt
ltxscomplexTypegt ltxssequencegt
ltxselement ref"recipient" /gt
ltxselement ref"order" /gt
lt/xssequencegt ltxsattribute
name"orderDate" type"xsstring" /gt
lt/xscomplexTypegt lt/xselementgt
8
ltxselement name "recipient"gt
ltxscomplexTypegt ltxssequencegt
ltxselement ref"name" /gt
ltxselement ref"street" /gt
ltxselement ref"city" /gt
ltxselement ref"state" /gt
ltxselement ref"postalCode" /gt
lt/xssequencegt ltxsattribute
name"country" type"xsstring" /gt
lt/xscomplexTypegt lt/xselementgt
9
ltxselement name "name" type"xsstring" /gt
ltxselement name "street" type"xsstring" /gt
ltxselement name "city" type"xsstring"
/gt ltxselement name "state"
type"xsstring" /gt ltxselement name
"postalCode" type"xsshort" /gt ltxselement
name "order"gt ltxscomplexTypegt
ltxssequencegt
ltxselement ref"cd" maxOccurs"unbounded"/gt
lt/xssequencegt
lt/xscomplexTypegt lt/xselementgt
10
ltxselement name"cd"gt
ltxscomplexTypegt ltxsattribute
name"artist" type"xsstring" /gt
ltxsattribute name"title" type"xsstring" /gt
lt/xscomplexTypegt lt/xselementgt lt/xssch
emagt
11
Three Major Uses Same as DTDs
  • 1. Validation
  • Code Generation
  • Communication

12
Better than DTDs
  • Good support for namespaces
  • Type Checking
  • XML Syntax

But Harder than DTDs
13
Validate.java
  • // Validate.java using Xerces
  • import java.io.
  • import org.xml.sax.ErrorHandler
  • import org.xml.sax.SAXException
  • import org.xml.sax.SAXParseException
  • import org.xml.sax.XMLReader
  • import org.xml.sax.InputSource
  • import org.xml.sax.helpers.XMLReaderFactory
  • import org.xml.sax.helpers.DefaultHandler
  • import java.io.

14
  • import javax.xml.parsers.SAXParser
  • import javax.xml.parsers.SAXParserFactory
  • import org.xml.sax.helpers.DefaultHandler
  • import org.xml.sax.SAXException
  • import org.xml.sax.InputSource
  • import org.xml.sax.SAXParseException

15
  • public class Validate extends DefaultHandler
  • public static boolean valid true
  • public void error(SAXParseException
    exception)
  • System.out.println("Received
    notification of a recoverable error."
    exception)
  • valid false
  • public void fatalError(SAXParseException
    exception)
  • System.out.println("Received
    notification of a non-recoverable error."
    exception)
  • valid false
  • public void warning(SAXParseException
    exception)
  • System.out.println("Received
    notification of a warning." exception)

16
  • public static void main (String argv )
  • if (argv.length ! 1)
  • System.err.println ("Usage java
    Validate filename.xml")
  • System.exit (1)
  • try
  • // get a parser
  • XMLReader
  • reader
    XMLReaderFactory.createXMLReader(

  • "org.apache.xerces.parsers.SAXParser")
  • // request validation
  • reader.setFeature("http//xml.org/s
    ax/features/validation",true)
  • reader.setFeature(

  • "http//apache.org/xml/features/validation/schema"
    ,true)
  • reader.setErrorHandler(new
    Validate())
  • // associate an InputSource object
    with the file name
  • InputSource inputSource new
    InputSource(argv0)

17
  • catch(org.xml.sax.SAXException e)
  • System.out.println("Error in
    parsing " e)
  • valid false
  • catch(java.io.IOException e)
  • System.out.println("Error in I/O "
    e)
  • System.exit(0)
  • System.out.println("Valid Document is "
    valid)

18
XML Document
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltitemList
  • xmlnsxsi'http//www.w3.org/2001/XMLSchema-inst
    ance'
  • xsinoNamespaceSchemaLocation"itemList.xsd"gt
  • ltitemgt
  • ltnamegtpenlt/namegt
  • ltquantitygt5lt/quantitygt
  • lt/itemgt
  • ltitemgt
  • ltnamegteraserlt/namegt
  • ltquantitygt7lt/quantitygt
  • lt/itemgt
  • ltitemgt
  • ltnamegtstaplerlt/namegt
  • ltquantitygt2lt/quantitygt
  • lt/itemgt
  • lt/itemListgt

19
XSDL Grammar itemList.xsd
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltxsdschema xmlnsxsd'http//www.w3.org/2001/XMLS
    chema'gt
  • ltxsdelement name"itemList"gt
  • ltxsdcomplexTypegt
  • ltxsdsequencegt
  • ltxsdelement ref"item"
  • minOccurs"0"
    maxOccurs"3"/gt
  • lt/xsdsequencegt
  • lt/xsdcomplexTypegt
  • lt/xsdelementgt

20
  • ltxsdelement name"item"gt
  • ltxsdcomplexTypegt
  • ltxsdsequencegt
  • ltxsdelement ref"name"/gt
  • ltxsdelement ref"quantity"/gt
  • lt/xsdsequencegt
  • lt/xsdcomplexTypegt
  • lt/xsdelementgt
  • ltxsdelement name"name" type"xsdstring"/gt
  • ltxsdelement name"quantity" type"xsdshort"/gt
  • lt/xsdschemagt

21
  • D..95-733\examples\XSDL\testinggtant run
  • Buildfile build.xml
  • run
  • Running Validate.java on itemList-xsd.xml
  • Valid Document is true

22
Purchase again (with Prefixes)
  • lt?xml version"1.0" encoding"UTF-8"?gt lt!--
    po.xml --gt
  • ltmynspurchaseOrder orderDate"07.23.2001"
  • xmlnsmyns"http//www.cds-r-us.com"
  • xmlnsxsi
  • "http//www.w3.org/2001/XMLSchema
    -instance"
  • xsischemaLocation
  • "http//www.cds-r-us.com
  • po.xsd"
  • gt

23
  • ltmynsrecipient country"USA"gt
  • ltmynsnamegtDennis Scannellt/mynsnamegt
  • ltmynsstreetgt175 Perry Lea Side
    Roadlt/mynsstreetgt
  • ltmynscitygtWaterburylt/mynscitygt
  • ltmynsstategtVTlt/mynsstategt
  • ltmynspostalCodegt05675Alt/mynspostalCodegt
  • lt/mynsrecipientgt

Note that there is a problem with this document.
24
  • ltmynsordergt
  • ltmynscd artist"Brooks Williams"
    title"Little Lion" /gt
  • ltmynscd artist"David Wilcox"
    title"What you whispered" /gt
  • lt/mynsordergt
  • lt/mynspurchaseOrdergt

25
XSDL Grammar po.xsd
  • lt?xml version"1.0" encoding"utf-8"?gt lt!--
    po.xsd --gt
  • ltxsschema
  • xmlnsxs"http//www.w3.org/2001/XMLSchema"
  • xmlns"http//www.cds-r-us.com"
  • targetNamespace"http//www.cds-r-us.com"
  • gt
  • ltxselement name"purchaseOrder"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement ref"recipient" /gt
  • ltxselement ref"order" /gt
  • lt/xssequencegt
  • ltxsattribute name"orderDate"
    type"xsstring" /gt
  • lt/xscomplexTypegt
  • lt/xselementgt

26
  • ltxselement name "recipient"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement ref"name" /gt
  • ltxselement ref"street" /gt
  • ltxselement ref"city" /gt
  • ltxselement ref"state" /gt
  • ltxselement ref"postalCode" /gt
  • lt/xssequencegt
  • ltxsattribute name"country"
    type"xsstring" /gt
  • lt/xscomplexTypegt
  • lt/xselementgt

27
  • ltxselement name "name" type"xsstring" /gt
  • ltxselement name "street" type"xsstring"
    /gt
  • ltxselement name "city" type"xsstring"
    /gt
  • ltxselement name "state" type"xsstring"
    /gt
  • ltxselement name "postalCode"
    type"xsshort" /gt
  • ltxselement name "order"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement ref"cd"
    maxOccurs"unbounded"/gt
  • lt/xssequencegt
  • lt/xscomplexTypegt
  • lt/xselementgt

28
  • ltxselement name"cd"gt
  • ltxscomplexTypegt
  • ltxsattribute name"artist"

  • type"xsstring" /gt
  • ltxsattribute name"title"
    type"xsstring" /gt
  • lt/xscomplexTypegt
  • lt/xselementgt
  • lt/xsschemagt

29
Running Validate
  • D..\examples\XSDL\testinggtant run
  • Buildfile build.xml
  • run
  • Running Validate.java on po.xml
  • Received notification of a recoverable
    error.org.xml.sax.SAXParseException
    cvc-datatype-valid.1.2.1 '05675A' is not a valid
    'integer' value.
  • Received notification of a recoverable
    error.org.xml.sax.SAXParseException
    cvc-type.3.1.3 The value '05675A' of element
    'mynspostalCode' is not valid.
  • Valid Document is false

30
Fix the error and run again
  • D\..\XSDL\testinggtant run
  • Buildfile build.xml
  • run
  • Running Validate.java on po.xml
  • Valid Document is true

31
From W3C XML Schema Part 2 DataTypes
32
Introduce a Namespace Error
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • lt!-- po.xml --gt
  • ltmynspurchaseOrder orderDate"07.23.2001"
  • xmlnsmyns"http//www.cds-r-us.edu"
  • xmlnsxsi
  • "http//www.w3.org/2001/XMLSchema-ins
    tance"
  • xsischemaLocation"http//www.cds-r-us.com
  • po.xsd"
  • gt

33
  • ltmynsrecipient country"USA"gt
  • ltmynsnamegtDennis Scannellt/mynsnamegt
  • ltmynsstreetgt
  • 175 Perry Lea Side Road
  • lt/mynsstreetgt
  • ltmynscitygtWaterburylt/mynscitygt
  • ltmynsstategtVTlt/mynsstategt
  • ltmynspostalCodegt05675lt/mynspostalCodegt
  • lt/mynsrecipientgt

34
  • ltmynsordergt
  • ltmynscd artist"Brooks Williams"
    title"Little Lion" /gt
  • ltmynscd artist"David Wilcox"
    title"What you whispered" /gt
  • lt/mynsordergt
  • lt/mynspurchaseOrdergt

35
And run validate
  • run
  • Running Validate.java on po.xml
  • Received notification of a recoverable
  • error.org.xml.sax.SAXParseException
    cvc-elt.1
  • Cannot find the declaration of element
    'mynspurchaseOrder'.
  • Valid Document is false

36
Code Generation
  • Run JAXB against the .xsd file
  • Code generated will present an API allowing us to
    process that style of
  • document

37
itemList.xsd again
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltxsdschema xmlnsxsd'http//www.w3.org/2001/XMLS
    chema'gt
  • ltxsdelement name"itemList"gt
  • ltxsdcomplexTypegt
  • ltxsdsequencegt
  • ltxsdelement ref"item"
  • minOccurs"0"
    maxOccurs"3"/gt
  • lt/xsdsequencegt
  • lt/xsdcomplexTypegt
  • lt/xsdelementgt

38
  • ltxsdelement name"item"gt
  • ltxsdcomplexTypegt
  • ltxsdsequencegt
  • ltxsdelement ref"name"/gt
  • ltxsdelement ref"quantity"/gt
  • lt/xsdsequencegt
  • lt/xsdcomplexTypegt
  • lt/xsdelementgt
  • ltxsdelement name"name" type"xsdstring"/gt
  • ltxsdelement name"quantity" type"xsdshort"/gt
  • lt/xsdschemagt

39
Run xjc
  • D..XSDL\testinggtxjc itemList.xsd
  • D\McCarthy\www\95-733\examples\XSDL\testinggtjava
    -jar D\jwsdp-1.1\jaxb-1.0\lib
  • \jaxb-xjc.jar itemList.xsd
  • parsing a schema...
  • compiling a schema...
  • generated\impl\ItemImpl.java
  • generated\impl\ItemListImpl.java
  • generated\impl\ItemListTypeImpl.java
  • generated\impl\ItemTypeImpl.java
  • generated\impl\NameImpl.java

40
  • generated\impl\QuantityImpl.java
  • generated\Item.java
  • generated\ItemList.java
  • generated\ItemListType.java
  • generated\ItemType.java
  • generated\Name.java
  • generated\ObjectFactory.java
  • generated\Quantity.java
  • generated\bgm.ser
  • generated\jaxb.properties

Write Java Code That uses NEW the api
41
The Ant build script used for these examples is
also XML
lt?xml version"1.0"?gt ltproject basedir"."
default"compile"gt ltpath
id"classpath"gt ltfileset
dir"D/jwsdp-1.1/saaj-1.1.1/lib"
includes".jar"/gt ltfileset
dir"D/jwsdp-1.1/jaxb-1.0/lib"
includes".jar"/gt ltfileset
dir"d/jwsdp-1.1/common/lib" includes".jar"/gt

42
  • ltfileset dir"D/jwsdp-1.1/jaxm-1.1.1/lib
    " includes".jar"/gt
  • ltfileset dir"D/jwsdp-1.1/bin"
    includes".jar" /gt
  • ltfileset dir"D/jwsdp-1.1/jaxp-1.2.2/lib
    " includes".jar"/gt
  • ltfileset dir"D/jwsdp-1.1/jaxp-1.2.2/lib
    /endorsed"
  • includes".jar"/gt
  • ltfileset dir"D/jwsdp-1.1/jwsdp-shared/l
    ib" includes".jar"/gt
  • ltfileset dir"D/jwsdp-1.1/jaxr-1.0_03/li
    b" includes".jar"/gt
  • ltfileset dir"D/jwsdp-1.1/jakarta-ant-1.
    5.1/lib" includes".jar"/gt
  • ltfileset dir"D/j2sdk1.4.1_01/lib"
    includes".jar"/gt
  • ltpathelement location"."/gt
  • lt/pathgt

43
  • lt!-- compile Java source files --gt
  • lttarget name"compile"gt
  • lt!-- compile all of the java sources --gt
  • ltecho message"Compiling the java source
    files..."/gt
  • ltjavac srcdir"." destdir"." debug"on"gt
  • ltclasspath refid"classpath" /gt
  • lt/javacgt
  • lt/targetgt
  • lttarget name"run"gt
  • ltecho message"Running Validate.java on
    po.xml"/gt
  • ltjava classname"Validate" fork"fasle"gt
  • ltarg value"po.xml"/gt
  • ltclasspath refid"classpath" /gt
  • lt/javagt
  • lt/targetgt
  • lt/projectgt

44
More details on XML Schemas
45
Comments in XSDL
  • ltxsschema
  • xmlnsxs"http//www.w3.org/2001/XMLSchema"
  • xmlns"http//www.cds-r-us.com"
  • targetNamespace"http//www.cds-r-us.com"
  • gt
  • ltxsannotationgt
  • ltxsdocumentationgtThis is a comment.
    lt/xsdocumentationgt
  • lt/xsannotationgt

46
Element Definitions
  • The Element element is used to define an element
  • The Element element may be empty
  • ltxselement name "name" type"xsstring"
    /gt
  • Or, may contain content
  • ltxselement name"cd"gt
  • ltxscomplexTypegt
  • ltxsattribute name"artist"
    type"xsstring" /gt
  • ltxsattribute name"title"
    type"xsstring" /gt
  • lt/xscomplexTypegt
  • lt/xselementgt

47
Element Definitions
  • The Element element may be empty and contain no
    other attributes
  • ltxselement name "purchaseOrder"/gt
  • This purchaseOrder element may contain anything
    (more elements and text)
  • DTD lt!ELEMENT purchaseOrder ANYgt

48
Simple Content
  • An element may be defined to hold only a number,
    word or text
  • ltxselement name "city" type"xsstring" /gt
  • DTD lt!ELEMENT city (PCDATA)gt

49
Complex Content
  • The element may contain child elements or
    attributes
  • ltxselement name"purchaseOrder"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement ref"recipient"
    /gt
  • ltxselement ref"order" /gt
  • lt/xssequencegt
  • ltxsattribute name"orderDate"
    type"xsstring" /gt
  • lt/xscomplexTypegt
  • lt/xselementgt

An indicator on how these elements are to be
combined sequence gtpredefined order
50
Place Holder Element Definitions
  • ltelement namepageBreakgt
  • ltcomplexTypegtlt/complexTypegt
  • lt/elementgt
  • No content is permitted
  • DTD lt!ELEMENT pageBreak EMPTYgt
  • lt!ATTLIST pageBreakgt

51
Namespace Issues
  • All element definitions belong to a target
    document-type namespace
  • If a prefix is used for schema elements (as we
    have done above) then we need to specify that
    prefix on datatypes to distinguish those defined
    in XSDL from those the author may define.
  • ltxsattribute name"orderDate"
    type"xsstring" /gt

52
Occurrence options
  • MinOccurs and MaxOccurs
  • Default(if not mentioned) MinOccurs MaxOccurs
    1
  • MinOccurs 0 element is optional
  • MaxOccurs unbounded infinity

53
Choices And a New Example
  • lt?xml version"1.0" encoding"utf-8"?gt lt!--
    addr.xsd --gt
  • ltschema
  • xmlns"http//www.w3.org/2001/XMLSchema"
  • xmlnsco"http//www.myCompany.com"
  • targetNamespace"http//www.myCompany.com"
  • gt
  • ltannotationgt
  • ltdocumentationgtThis is the company address
    XSDL document. lt/documentationgt
  • lt/annotationgt

comment
54
ltelement name"addr"gt
ltcomplexTypegt ltchoicegt
ltelement ref"codowntown" /gt
ltelement ref"couptown" /gt
lt/choicegt lt/complexTypegt
lt/elementgt
Downtown or uptown
55
ltelement name "downtown"gt
ltcomplexTypegt ltsequencegt
ltelement ref"costreet" /gt
lt/sequencegt lt/complexTypegt
lt/elementgt ltelement name "uptown"gt
ltcomplexTypegt ltsequencegt
ltelement ref"costreet" /gt
ltelement ref"coapt" /gt
lt/sequencegt lt/complexTypegt
lt/elementgt
56
ltelement name "street" type"string" /gt
ltelement name "apt" type"integer" /gt
lt/schemagt
57
lt?xml version"1.0" encoding"UTF-8"?gt lt!
addr.xml --gt ltaddr xmlns"http//www.myCom
pany.com" xmlnsxsi"http//www.w3.org/2001/
XMLSchema-instance" xsischemaLocation"http
//www.myCompany.com
address.xsd" gt ltdowntowngt
ltstreetgtFifth Avenuelt/streetgt lt/downtowngt
lt/addrgt java Validate addr.xml Valid
document is true
58
Choices
  • ltelement name"addr"gt
  • ltcomplexTypegt
  • ltchoice minOccurs "0"gt
  • ltelement ref"codowntown" /gt
  • ltelement ref"couptown" /gt
  • lt/choicegt
  • lt/complexTypegt
  • lt/elementgt

Choose 1 or none
DTD lt!ELEMENT addr (downtown uptown)? gt
59
Choices
Choose 1 and then as many as you like from the
same group
  • ltelement name"addr"gt
  • ltcomplexTypegt
  • ltchoice maxOccurs unbounded"gt
  • ltelement ref"codowntown" /gt
  • ltelement ref"couptown" /gt
  • lt/choicegt
  • lt/complexTypegt
  • lt/elementgt

DTD lt!ELEMENT addr (downtown uptown) gt
60
Embedded Groups
Required
  • ltsequencegt
  • ltelement ref doctitle/gt
  • ltchoice minOccurs0 maxOccursunboundedgt
  • ltelement refdocpara/gt
  • ltelement refdoclist/gt
  • lt/choicegt
  • lt/sequencegt

Choose as many as you like including none
DTD (title, (para list))
61
Mixed Content
  • ltelement nameparagt
  • ltcomplexType mixedtruegt
  • ltchoice minOccurs0 maxOccursunbounded
    gt
  • ltelement refdocemph /gt
  • ltelement refdocname /gt
  • lt/choicegt
  • lt/complexTypegt
  • lt/elementgt

DTD lt!ELEMENT para (PCDATA emph name)gt
Text is always optional.
62
Attributes
  • May never contain element tags or sub-elements
  • So, attribute type values will always be simple
    types
  • By default, attributes are optional
  • ltxsattribute name"orderDate"
    type"xsstring" /gt

63
Attributes
  • Can be required, prohibited, optional
  • ltxsattribute name"orderDate" type"xsstring
    userequired /gt
  • After removing the attribute .
  • D..\95-733\examples\XSDL\testinggtjava Validate
    po.xml
  • Received notification of a recoverable
    error.org.xml.sax.SAXParseException cvc
    complex-ty
  • pe.4 Attribute 'orderDate' must appear on
    element 'purchaseOrder'.
  • Valid Document is false

64
Attribute Types
  • ltxsattribute name"orderDate" type"xsdate
    userequired /gt
  • Does not validate
  • ltpurchaseOrder orderDate"07.23.2001"
  • Validates
  • ltpurchaseOrder orderDate"1955-12-17"
Write a Comment
User Comments (0)
About PowerShow.com