Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

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
  2. 2. Thank You ASF n Xerces-J-Dev n Xerces-J-User n
  3. 3. Outline Overview n 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 n JSP, Servlets, Ant, Struts, Server Frameworks n n
  5. 5. 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=''> <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=''> <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=''> <element name='friends'> <simpleType> <list itemType='string'/> </simpleType> </element> </schema> Union n
  18. 18. Example of complex type <schema xmlns=''> <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=‘’> <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=''> <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='' > <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='' > <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=''> <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=''> <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=''> <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=‘ .xsd’/>
  36. 36. Target Namespaces To put a set of definitions into a n namespace… <schema xmlns='' targetNamespace=' 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=' xmlns:beers=' rial/importee' targetNamespace=' 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='‘ xmlns:importedBeerSchema=' /Schemas/Tutorial/importee‘ targetNamespace=' Tutorial/importer‘ elementFormDefault=‘qualified’> <import namespace=' 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 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=''> <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=‘ 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='' xmlns:ann=' al/annonate' targetNamespace=' 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='' 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> </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=''> <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=''> <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=‘’ xsi:noNamespaceSchemaLocation='simple.xsd'> 25 </withdraw> <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> n <bank:withdraw xmlns=‘’ xmlns:xsi=‘’ xsi:schemaLocation=‘ 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! n n