How to develop
Syntax and XML Schema

            Ted Leung
    Chairman, ASF XML PMC
Principal, Sauria Associates, LLC
  ...
Thank You

    ASF
n

    Xerces-J-Dev
n

    Xerces-J-User
n
Outline
    Overview
n

    xml.apache.org project
n

    Business / Technical motivation for XML
n
    Schema
    Coverag...
Apache Software Foundation
    Non-profit foundation
n
        volunteers
    n

    Open Source Software
n

    Apache Li...
xml.apache.org
    Part of the Apache Software Foundation
n

    Open Source XML processing
n

    components
        XML ...
Business Motivation
    XML is a language of agreements
n

    We needed a way to specify those
n

    agreements in more ...
Technical Motivation
    Three major needs leading up to XML
n

    Schema
        Strong data typing of element content a...
When would you use Schema?
    You have rich data types [typing]
n

    You need open content models
n

    [wildcards]
  ...
Example Schema Applications
    SOAP / WSDL
n

    XSLT 2
n

    XForms
n

    Many more to come
n
XML Schema Specification
    W3C Recommendation as of 5/2/2001
n

    Three documents:
n

        XML Schema Part 0: Prime...
Type System Design Features
    Elements and attributes have explicit
n

    types
    Types can be defined independently
...
Two kinds of types
    Simple types
n

        describe character data
    n


    Complex types
n

        Can have attri...
Example of simple type
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
<element name='withdraw' type=quot;integer“/>
</s...
Simple types
    Lexical Space
n

    Value Space
n

    Facets
n

    Primitive built-in types
n

        XML 1.0 types
 ...
Creating new simple types
    Restriction
n
         <schema
    n
         xmlns='http://www.w3.org/2001/XMLSchema'>
    ...
Facets
    Numeric range restriction
n

        Miniumum and maximum
    n

        Inclusive / exclusive
    n


    Enum...
Atomic vs non-Atomic types
    Atomic Types
n

    List
n
        <schema
    n

        xmlns='http://www.w3.org/2001/XML...
Example of complex type
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
 <element name='withdraw'>
  <complexType>
   <s...
Complex types
    Carry attributes
n

    Can have child element content
n

    Can be derived from simple types
n
Content Models
    Complex types have content models to
n

    describe nested elements, etc.
    Content Model types
n

 ...
Element Only Content
    <schema
n

        xmlns=‘http://www.w3.org/2001/XMLSchema’>
     <complexType name=‘person’>
   ...
Compositors
    Three compositors
n

        <sequence>
    n

        <choice>
    n

        <all>
    n
Mixed Content
    Stricter than XML 1.0 – order and
n

    number of child elements counts
    Attribute on complexType or...
Example of Mixed Content
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
 <element name='boilerplate'>
  <complexType mi...
Empty Content
 <schema
  xmlns='http://www.w3.org/2001/XMLSchema' >
  <complexType name=‘void’>
   <attribute name=‘size’ ...
minOccurs/maxOccurs
    How do I specify how many times an element
n
    occurs?

    <schema
      xmlns='http://www.w3.o...
Attributes
    Use any simple type
n


    <attribute name=‘delayed’ type=‘boolean’
               use=‘optional’ default=...
ComplexType Extension
    Add to end of type
n


<schema xmlns='http://www.w3.org/2001/XMLSchema'>
   <complexType name='p...
ComplexType Extension
<complexType name='parentType'>
 <complexContent>
  <extension base='personType'>
     <sequence>
  ...
ComplexType Restriction 1
    Must state restriction
n


<schema xmlns='http://www.w3.org/2001/XMLSchema'>
 <complexType n...
ComplexType Restriction 2
<complexType name='orphanType'>
  <complexContent>
   <restriction base='personType'>
    <seque...
Modularity constructs
    <group>
n

        For Content Models
    n


    <attributeGroup>
n

        For attributes
   ...
Modularity Example
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
 <group name='phoneNumber'>
   <sequence>
    <elemen...
Modularity vs inheritance
    Kohsuke Kawaguchi
n

    Model groups simulate inheritance,
n

    since they nest
    Restr...
Include
    For physical modularity of a schema
n

    Definitions in the same target
n

    namespace
    <include
n

   ...
Target Namespaces
    To put a set of definitions into a
n

    namespace…

<schema xmlns='http://www.w3.org/2001/XMLSchem...
Import
    Mix types from different namespaces
n

    Simple types can be used
n

        Must be global
    n

    Comple...
Import Example 1
<schema
  xmlns='http://www.w3.org/2001/XMLSchema
  xmlns:beers='http://www.sauria.com/Schemas/Tuto
  ria...
Import Example 2
<schema
  xmlns='http://www.w3.org/2001/XMLSchema‘
  xmlns:importedBeerSchema='http://www.sauria.com
  /S...
XML Schema Instance NS
    XML Schema Instance Namespace
n
          URI is
    n

          http://www.w3.org/2001/XMLSch...
xsi:nil Example
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
 <complexType name='personType'>
  <sequence>
   <elemen...
Wildcards
    How do I leave a schema open for
n
    extension?
    “Any element from namespace x,y or z”
n

    “Any elem...
Wildcard Example 1
<schema xmlns='http://www.w3.org/2001/XMLSchema'
  xmlns:ann='http://www.sauria.com/Schemas/Tutori
  al...
Wildcard Example 2
<complexType name='annotation'>
 <sequence>
  <any namespace='http://www.w3.org/1999/xhtml'
 processCon...
Annotation
    A standard way to document schemas
n

    <documentation> – for humans
n

    <appinfo> – for programs
n

 ...
Uniqueness
    “I want to ensure that the value of an
n

    element <foo> is unique”
        In what scope? There may be ...
Unique Example 1
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
 <complexType name='bookType'>
   <sequence>
    <eleme...
Unique Example 2
 <unique name='isbn'>
  <selector xpath='books/book'/>
  <field xpath='ISBN'/>
 </unique>
</schema>
Keys
<schema xmlns='http://www.w3.org/2001/XMLSchema'>
 <complexType name='bookType'>
  <sequence>
   <element name='title...
Keys Example 1
<element name='books'>
 <complexType>
  <sequence>
     <element name='book' type='bookType'
 maxOccurs='un...
Keys Example 2
<unique name='isbn'>
   <selector xpath='books/book'/>
   <field xpath='ISBN'/>
 </unique>

<key name='card...
Referencing a schema
    <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
n
    <withdraw
    xmlns:xsi=‘http://www....
Tool support
    Xerces-J [ REC ]
n

    XSV [ REC ]
n

    Oracle XML Parser [ PR ]
n

    MSXML 4.0 [ PR ]
n
Cool Tool Tricks
    Use XSLT on your Schema
n

    Data Binding
n
Alternatives to XML Schema
    Some people feel XML Schema is too
n

    complicated
    Relax => TREX => Relax NG
n

    ...
Relax NG
    Uses XML Instance Syntax
n

    Supports namespaces
n

    Much simpler than XML Schema
n

    More orthogona...
Relax NG Example
    <element name=‘book’ dataTypeLibrary=‘…’>
n
     <element name=‘title’>
       <data type=‘string’>
 ...
Thank You!
    http://xml.apache.org
n

    twl@apache.org
n
Upcoming SlideShare
Loading in …5
×

IQPC Canada XML 2001: How to develop Syntax and XML Schema

1,272 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,272
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

IQPC Canada XML 2001: How to develop Syntax and XML Schema

  1. 1. How to develop Syntax and XML Schema Ted Leung Chairman, ASF XML PMC Principal, Sauria Associates, LLC twl@sauria.com
  2. 2. Thank You ASF n Xerces-J-Dev n Xerces-J-User n
  3. 3. Outline Overview n xml.apache.org project n Business / Technical motivation for XML n Schema Coverage of Key XML Schema Features n Alternative Schema Languages n
  4. 4. Apache Software Foundation Non-profit foundation n volunteers n Open Source Software n Apache License n Not viral like GPL n Commercial Use is fine n Projects n Web Server n jakarta.apache.org n JSP, Servlets, Ant, Struts, Server Frameworks n xml.apache.org n
  5. 5. xml.apache.org Part of the Apache Software Foundation n Open Source XML processing n components XML Parser [Xerces] (Java, C++, Perl) n XSLT processor [Xalan] (Java, C++) n XSL Formatting Objects [FOP] n SVG [Batik] n Cocoon n SOAP [Apache-SOAP / Axis] n
  6. 6. Business Motivation XML is a language of agreements n We needed a way to specify those n agreements in more detail – raises the level of discourse between applications We needed a way for machines to be n able to do more with those agreements
  7. 7. Technical Motivation Three major needs leading up to XML n Schema Strong data typing of element content and n attributes – push more validation into the XML infrastructure Integration of namespaces into grammars n Use of XML syntax to describe the n grammar
  8. 8. When would you use Schema? You have rich data types [typing] n You need open content models n [wildcards] You need to combine data from multiple n organzations [namespaces] You are mapping from database n [uniqueness]
  9. 9. Example Schema Applications SOAP / WSDL n XSLT 2 n XForms n Many more to come n
  10. 10. XML Schema Specification W3C Recommendation as of 5/2/2001 n Three documents: n XML Schema Part 0: Primer n XML Schema Part 1: Structures n XML Schema Part 2: Datatypes n Support in Xerces-J 1.4+ n
  11. 11. Type System Design Features Elements and attributes have explicit n types Types can be defined independently n Definitions n Global n Local/Anonymous n
  12. 12. Two kinds of types Simple types n describe character data n Complex types n Can have attributes n Can have content models (elements) n
  13. 13. Example of simple type <schema xmlns='http://www.w3.org/2001/XMLSchema'> <element name='withdraw' type=quot;integer“/> </schema>
  14. 14. Simple types Lexical Space n Value Space n Facets n Primitive built-in types n XML 1.0 types n String, boolean, numbers, dates, times n
  15. 15. Creating new simple types Restriction n <schema n xmlns='http://www.w3.org/2001/XMLSchema'> <element name='withdraw'> <simpleType> <restriction base='integer'> <minInclusive value=quot;0quot;/> </restriction> </simpleType> </element> </schema> Some built in types are restrictions of n primitive types
  16. 16. Facets Numeric range restriction n Miniumum and maximum n Inclusive / exclusive n Enumeration n Explicit enumeration n Pattern – regular expressions n Constrains strings n
  17. 17. Atomic vs non-Atomic types Atomic Types n List n <schema n xmlns='http://www.w3.org/2001/XMLSchema'> <element name='friends'> <simpleType> <list itemType='string'/> </simpleType> </element> </schema> Union n
  18. 18. Example of complex type <schema xmlns='http://www.w3.org/2001/XMLSchema'> <element name='withdraw'> <complexType> <simpleContent> <extension base='integer'> <attribute name='currency‘ type='string' default='us'/> </extension> </simpleContent> </complexType> </element> </schema>
  19. 19. Complex types Carry attributes n Can have child element content n Can be derived from simple types n
  20. 20. Content Models Complex types have content models to n describe nested elements, etc. Content Model types n SimpleContent – just content n element only content n ComplexContent – type derivation n Mixed Content – elements and content n Empty content n
  21. 21. Element Only Content <schema n xmlns=‘http://www.w3.org/2001/XMLSchema’> <complexType name=‘person’> <sequence> <element name=‘name’ type=‘string’/> <element name=‘age’ type=‘positiveInteger’> </sequence> </complexType> </schema>
  22. 22. Compositors Three compositors n <sequence> n <choice> n <all> n
  23. 23. Mixed Content Stricter than XML 1.0 – order and n number of child elements counts Attribute on complexType or n complexContent
  24. 24. Example of Mixed Content <schema xmlns='http://www.w3.org/2001/XMLSchema'> <element name='boilerplate'> <complexType mixed='true'> <all> <element name='heading' type='string'/> <element name='version' type='decimal'/> <element name='email' type='string'/> </all> </complexType> </element> </schema>
  25. 25. Empty Content <schema xmlns='http://www.w3.org/2001/XMLSchema' > <complexType name=‘void’> <attribute name=‘size’ type=‘integer’/> </complexType> </schema>
  26. 26. minOccurs/maxOccurs How do I specify how many times an element n occurs? <schema xmlns='http://www.w3.org/2001/XMLSchema' > <complexType name=‘friend’> <element name=‘lastName’ type=‘string’/> <element name=‘firstName’ type=‘string’/> </complexType> <element name=‘friends’> <sequence> <element name=‘friend’ minOccurs=‘0’ maxOccurs=‘unbounded’/> </sequence> </schema>
  27. 27. Attributes Use any simple type n <attribute name=‘delayed’ type=‘boolean’ use=‘optional’ default=‘false’/> <attribute name=‘ranking’ type=‘positiveInteger’ use=‘required’/>
  28. 28. ComplexType Extension Add to end of type n <schema xmlns='http://www.w3.org/2001/XMLSchema'> <complexType name='personType'> <sequence> <element name='name' type='string'/> <element name='father' type='string'/> </sequence> </complexType>
  29. 29. ComplexType Extension <complexType name='parentType'> <complexContent> <extension base='personType'> <sequence> <element name='child' type='string'/> </sequence> </extension> </complexContent> </complexType> <element name='person' type='personType'/> <element name='parent' type='parentType'/> </schema>
  30. 30. ComplexType Restriction 1 Must state restriction n <schema xmlns='http://www.w3.org/2001/XMLSchema'> <complexType name='personType'> <sequence> <element name='name' type='string'/> <element name='father' type='string'/> </sequence> </complexType>
  31. 31. ComplexType Restriction 2 <complexType name='orphanType'> <complexContent> <restriction base='personType'> <sequence> <element name='name' type='string'/> </sequence> </restriction> </complexContent> </complexType> <element name='person' type='personType'/> <element name='orphan' type='orphanType'/> </schema>
  32. 32. Modularity constructs <group> n For Content Models n <attributeGroup> n For attributes n
  33. 33. Modularity Example <schema xmlns='http://www.w3.org/2001/XMLSchema'> <group name='phoneNumber'> <sequence> <element name='areaCode' type='positiveInteger'/> <element name='number' type='string'/> </sequence> </group> <attributeGroup name='callingAttributes'> <attribute name='callingCard' type='string'/> </attributeGroup> <element name='ISP'> <complexType> <group ref='phoneNumber'/> <attributeGroup ref='callingAttributes'/> </complexType> </element> </schema>
  34. 34. Modularity vs inheritance Kohsuke Kawaguchi n Model groups simulate inheritance, n since they nest Restriction forces you to write it all out, n so this is a model group as well Checking model groups is much easier n than checking inheritance
  35. 35. Include For physical modularity of a schema n Definitions in the same target n namespace <include n schemaLocation=‘http://www.schemas.com/fragment .xsd’/>
  36. 36. Target Namespaces To put a set of definitions into a n namespace… <schema xmlns='http://www.w3.org/2001/XMLSchema' targetNamespace='http://www.sauria.com/Schemas/Tutori al/target'> <element name='withdraw' type=quot;integer“/> </schema>
  37. 37. Import Mix types from different namespaces n Simple types can be used n Must be global n Complex types can be used n Named, global types only n Import element must appear first in n schema Can also import and redefine imported n items
  38. 38. Import Example 1 <schema xmlns='http://www.w3.org/2001/XMLSchema xmlns:beers='http://www.sauria.com/Schemas/Tuto rial/importee' targetNamespace='http://www.sauria.com/Schemas/ Tutorial/importee‘ elementFormDefault=‘qualified’> <complexType name='importedBeer'> <sequence> <element name='color' type='string'/> <element name='alcohol' type='decimal'/> </sequence> </complexType> </schema>
  39. 39. Import Example 2 <schema xmlns='http://www.w3.org/2001/XMLSchema‘ xmlns:importedBeerSchema='http://www.sauria.com /Schemas/Tutorial/importee‘ targetNamespace='http://www.sauria.com/Schemas/ Tutorial/importer‘ elementFormDefault=‘qualified’> <import namespace='http://www.sauria.com/Schemas/Tutori al/importee‘ schemaLocation='importee.xsd'/> <element name='beer‘ type='importedBeerSchema:importedBeer'/> </schema>
  40. 40. XML Schema Instance NS XML Schema Instance Namespace n URI is n http://www.w3.org/2001/XMLSchema-instance Prefix is xsi n type n Force an element to be associated with a n particular type <person xsi:type=‘parent’/> n nil n For values that can be nil/null, specify the nil n value.
  41. 41. xsi:nil Example <schema xmlns='http://www.w3.org/2001/XMLSchema'> <complexType name='personType'> <sequence> <element name='name' type='string'/> <element name='father' type='string'/> </sequence> </complexType> <element name='person' type='personType' nillable='true'/> </schema> <person xmlns:xsi=‘http://www.w3.org/2001/XMLSchema- instance’ xsi:noNamespaceSchemaLocation='nil.xsd' xsi:nil='true'/>
  42. 42. Wildcards How do I leave a schema open for n extension? “Any element from namespace x,y or z” n “Any element from a namespace n besides this one” “Any element from a schema in no n namespace” “Any element from any namespace” n Similarly for attributes n
  43. 43. Wildcard Example 1 <schema xmlns='http://www.w3.org/2001/XMLSchema' xmlns:ann='http://www.sauria.com/Schemas/Tutori al/annonate' targetNamespace='http://www.sauria.com/Schemas/ Tutorial/annonate‘ elementFormDefault=‘qualified’> <complexType name='annotated'> <sequence> <element name='partName' type='string'/> <element name='partNo' type='integer'/> <element name='annotation' type='ann:annotation'/> </sequence> </complexType>
  44. 44. Wildcard Example 2 <complexType name='annotation'> <sequence> <any namespace='http://www.w3.org/1999/xhtml' processContents='lax'/> </sequence> </complexType> <element name='part' type='ann:annotated'/> </schema>
  45. 45. Annotation A standard way to document schemas n <documentation> – for humans n <appinfo> – for programs n <annotation> n <documentation>text here </documentation> <appinfo> <rdf-loc>http://www.schemas.com/rdf </rdf-loc> </appinfo> </annotation>
  46. 46. Uniqueness “I want to ensure that the value of an n element <foo> is unique” In what scope? There may be two n elements named <foo>, each children of two different elements Specify the scope using XPath n
  47. 47. Unique Example 1 <schema xmlns='http://www.w3.org/2001/XMLSchema'> <complexType name='bookType'> <sequence> <element name='title' type='string'/> <element name='ISBN' type='string'/> </sequence> </complexType> <element name='books'> <complexType> <sequence> <element name='book' type='bookType' maxOccurs='unbounded'/> </sequence> </complexType> </element>
  48. 48. Unique Example 2 <unique name='isbn'> <selector xpath='books/book'/> <field xpath='ISBN'/> </unique> </schema>
  49. 49. Keys <schema xmlns='http://www.w3.org/2001/XMLSchema'> <complexType name='bookType'> <sequence> <element name='title' type='string'/> <element name='ISBN' type='string'/> </sequence> </complexType> <complexType name='cardCatalog'> <sequence> <element name='cardNumber' type='string'/> </sequence> </complexType>
  50. 50. Keys Example 1 <element name='books'> <complexType> <sequence> <element name='book' type='bookType' maxOccurs='unbounded'/> <element name='catalogCard' type='cardCatalog' maxOccurs='unbounded'/> </sequence> </complexType> </element>
  51. 51. Keys Example 2 <unique name='isbn'> <selector xpath='books/book'/> <field xpath='ISBN'/> </unique> <key name='card'> <selector xpath='books/book'/> <field xpath='ISBN'/> </key> <keyref name='cardRef‘ refer=‘card’> <selector xpath='books/catalogCard'/> <field xpath='cardNumber'/> </keyref> </schema>
  52. 52. Referencing a schema <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> n <withdraw xmlns:xsi=‘http://www.w3.org/2001/XMLSchema-instance’ xsi:noNamespaceSchemaLocation='simple.xsd'> 25 </withdraw> <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> n <bank:withdraw xmlns=‘http://www.schemas.com/bank’ xmlns:xsi=‘http://www.w3.org/2001/XMLSchema-instance’ xsi:schemaLocation=‘http://www.schemas.com/bank simple.xsd'> 25 </bank:withdraw>
  53. 53. Tool support Xerces-J [ REC ] n XSV [ REC ] n Oracle XML Parser [ PR ] n MSXML 4.0 [ PR ] n
  54. 54. Cool Tool Tricks Use XSLT on your Schema n Data Binding n
  55. 55. Alternatives to XML Schema Some people feel XML Schema is too n complicated Relax => TREX => Relax NG n Relax (Murata Makoto) n Relax Verifier n TREX (James Clark) n JTREX n Relax NG (OASIS) n Jing n
  56. 56. Relax NG Uses XML Instance Syntax n Supports namespaces n Much simpler than XML Schema n More orthogonal than XML Schema n Can use XML Schema datatypes n Doesn’t support uniqueness, inheritance n
  57. 57. Relax NG Example <element name=‘book’ dataTypeLibrary=‘…’> n <element name=‘title’> <data type=‘string’> </element> <element name=‘quantity’> <data type=‘integer’> </element> </element> <element name=‘books’> <oneOrMore> <ref name=‘book’/> </oneOrMore> </element>
  58. 58. Thank You! http://xml.apache.org n twl@apache.org n

×