Your SlideShare is downloading. ×
0
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Relax NG, a Schema Language for XML
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Relax NG, a Schema Language for XML

2,700

Published on

Here is a presentation I gave on Relax NG, a schema language for XML, that I gave at OSCON 2003.

Here is a presentation I gave on Relax NG, a schema language for XML, that I gave at OSCON 2003.

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,700
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
24
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. RELAX NG A Schema Language for XML Michael Fitzgerald [email_address] Wy’east Communications
  • 2. Introductions <ul><li>A brief, swift, technical overview of RELAX NG </li></ul><ul><li>Some comparisons with DTDs and W3C XML Schema </li></ul><ul><li>As John Cowan has said: “Once RELAX NG crosses the ‘blood-brain barrier,’ you can never go back!” </li></ul>
  • 3. What Is RELAX NG? <ul><li>RELAX NG is a schema language for XML </li></ul><ul><li>A schema language for XML describes constraints for a vocabulary beyond ordinary XML syntax </li></ul><ul><li>RELAX NG is simple, intuitive, elegant, easy to use and learn, and has a foundation in finite tree automata </li></ul><ul><li>RELAX NG is being forwarded as part of ISO/IEC 19757 Document Schema Definition Languages or DSDL (see http://www.dsdl.org) </li></ul>
  • 4. When & Who? <ul><li>Version 1.0 specs were developed by the RELAX NG technical committee at OASIS between April and December 2001 </li></ul><ul><li>Merges Murata Makoto’s RELAX and James Clark’s TREX </li></ul><ul><li>James Clark is chair of the RELAX NG technical committee </li></ul>
  • 5. An Elegant Alternative <ul><li>Murata Makoto coauthored the RELAX NG tutorial and specification with James Clark </li></ul><ul><li>Offers an XML as well as a compact, non-XML syntax </li></ul><ul><li>Committed to simplicity, modularity, composability </li></ul><ul><li>No side effects, no PSVI </li></ul><ul><li>An alternative to the dominant W3C XML Schema </li></ul>
  • 6. XML Schema Competition? <ul><li>Is RELAX NG poised to threaten XML Schema’s dominance? No. </li></ul><ul><li>Will RELAX NG replace XML Schema? No. </li></ul><ul><li>Will RELAX NG continue to attract seasoned schema developers based on word of mouth? Probably. </li></ul>
  • 7. A Few Preliminaries <ul><li>Patterns describe content and structure of instances </li></ul><ul><li>An instance of a schema is a document that complies with that schema </li></ul><ul><li>Most RELAX NG patterns can act as a document element for the schema </li></ul><ul><li>Structure namespace also denotes version: http://relaxng.org/ns/structure/1.0 </li></ul>
  • 8. DTDs & RELAX NG <ul><li>RELAX NG is an evolution of the DTD </li></ul><ul><li>Elements and other structures are defined, not declared </li></ul><ul><li>No concept of associating a schema with an instance, as with a document type declaration, as in: </li></ul><ul><li><!DOCTYPE date SYSTEM &quot;date.dtd&quot;> </li></ul>
  • 9. Element Definitions <ul><li>The <element> element </li></ul><ul><li>Must have a name attribute or a <name> child element </li></ul><ul><li>In compact syntax, defined with an element keyword </li></ul><ul><li>XML Schema likewise has an <xs:element> element </li></ul>
  • 10. For Example, Elements & Schemas <ul><li>Following is an element definition which is also a complete schema in XML syntax ( date.rng ): </li></ul><ul><li><element name=&quot;date&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> </li></ul><ul><li> <text/> </li></ul><ul><li></element> </li></ul><ul><li>Compact syntax, namespace assumed by default ( date.rnc ): </li></ul><ul><li>element date { text } </li></ul>
  • 11. Validating with Jing <ul><li>Instance ( date.xml ): </li></ul><ul><li><date>2003-07-09</date> </li></ul><ul><li>Jing is a multi-platform RELAX NG validator, written by James Clark, in Java </li></ul><ul><li>Validation the element examples: </li></ul><ul><li>jing date.rng date.xml </li></ul><ul><li>jing –c date.rnc date.xml </li></ul>
  • 12. Other RELAX NG Tools <ul><li>James Clark’s Trang, a schema translator ( http://thaiopensource.com/relaxng/trang.html ) </li></ul><ul><li>Sun’s Multi-schema Validator or MSV ( http://wwws.sun.com/software/xml/developers/ multischema/ ) </li></ul><ul><li>Asami Tomoharu’s Relaxer schema compiler ( http://www.relaxer.org ) </li></ul>
  • 13. Adding an Attribute <ul><li>Content models formed by simple nesting </li></ul><ul><li>The < attribute> element is an example </li></ul><ul><li>In XML syntax, < text/> is assumed as a child of < attribute> and can be left out </li></ul><ul><li>Compact syntax, however, requires the text keyword </li></ul><ul><li>XML Schema likewise uses an <xs:attribute> element </li></ul>
  • 14. Attributes Examples <ul><li>XML syntax ( att.rng ): </li></ul><ul><li><element name=&quot;date&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> </li></ul><ul><li> <attribute name=&quot;type&quot;/> </li></ul><ul><li> <text/> </li></ul><ul><li></element> </li></ul><ul><li>Compact syntax ( att.rnc ): </li></ul><ul><li>element date { </li></ul><ul><li> attribute type { text }, </li></ul><ul><li> text </li></ul><ul><li>} </li></ul><ul><li>Match ( att.xml ): </li></ul><ul><ul><li><date type=&quot;ISO&quot;>2003-07-09</date> </li></ul></ul>
  • 15. Empty Elements <ul><li>Empty elements in XML may have attributes but no text or child element content </li></ul><ul><li>Element definitions may not be entirely empty </li></ul><ul><li>If no attributes are defined, must use <empty/> or empty in the content of the element definition </li></ul><ul><li>XML Schema signals empty content by the absence of a content model </li></ul>
  • 16. Empty Elements Examples <ul><li>XML syntax ( empty.rng ): </li></ul><ul><li><element name=&quot;br&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> </li></ul><ul><li><empty/> </li></ul><ul><li></element> </li></ul><ul><li>Compact ( empty.rnc ) : </li></ul><ul><li>element br {empty} </li></ul><ul><li>XML syntax ( image.rng ): </li></ul><ul><li><element name=&quot;image&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> </li></ul><ul><li><attribute name=&quot;source&quot;/> </li></ul><ul><li></element> </li></ul><ul><li>Compact ( image.rnc ): </li></ul><ul><li>element image {attribute source {text}} </li></ul><ul><li>Match empty.xml , image.xml </li></ul>
  • 17. Namespaces <ul><li>In RNG, the ns attribute defines the namespace that the pattern should match </li></ul><ul><li>ns namespace matches a namespace defined in the instance with xmlns </li></ul><ul><li>ns is inherited by child elements </li></ul><ul><li>xmlns declares, ns defines the matching namespace </li></ul><ul><li>Compact syntax uses default and namespace keywords </li></ul>
  • 18. Namespaces Examples <ul><li>XML syntax ( ns.rng ): </li></ul><ul><li><element name=&quot;date&quot; ns=&quot;http://www.wyeast.net/date&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> <text/> </element> </li></ul><ul><li>Compact ( ns.rnc ): </li></ul><ul><li>default namespace = &quot;http://www.wyeast.net/date&quot; element date {text} </li></ul><ul><li>Match ( ns.xml ): </li></ul><ul><li><date xmlns=&quot;http://www.wyeast.net/date&quot;>2003-07-09</date> </li></ul>
  • 19. Occurrence Constraints <ul><li><optional> is equivalent to ? (zero or one) in DTDs </li></ul><ul><li><oneOrMore> is equivalent to + in DTDs </li></ul><ul><li><zeroOrMore> is equivalent to * in DTDs </li></ul><ul><li>? and + and * work in RNC </li></ul><ul><li>RELAX NG does not have minOccurs and maxOccurs equivalents </li></ul>
  • 20. One or More Examples <ul><li>XML syntax ( dates.rng ): </li></ul><ul><li><element name=&quot;dates&quot; xmlns=“http://relaxng.org/ns/structure/1.0&quot;> <oneOrMore> <element name=&quot;date&quot;><text/></element> </oneOrMore> </element> </li></ul><ul><li>Compact ( dates.rnc ): </li></ul><ul><li>element dates {element date {text}*} </li></ul><ul><li>Match ( dates.xml ): </li></ul><ul><li><dates> <date>2003-07-07</date> <date>2003-07-08</date> <date>2003-07-09</date> </date> </li></ul>
  • 21. choice & group <ul><li><choice> matches instances with any one of its children </li></ul><ul><li>Compact uses | as in DTDs </li></ul><ul><li><group> matches instances with all of its children </li></ul><ul><li>Compact uses () as in DTDs </li></ul><ul><li>XML Schema also uses xs:choice and xs:group </li></ul>
  • 22. choice & group Examples <ul><li>XML syntax ( instant.rng ): </li></ul><ul><li><element name=&quot;instant&quot;> <choice> <group> <element name=&quot;date&quot;><text/></element> <element name=&quot;time&quot;><text/></element> </group> <element name=&quot;date-time&quot;><text/></element> </choice> </element> </li></ul><ul><li>Compact ( instant.rnc ): </li></ul><ul><li>element instant { (element date {text}, element time {text}) | element date-time {text}} </li></ul>
  • 23. Definitions <ul><li>Create named definitions with <define> </li></ul><ul><li>Can refer to named definition with <ref> </li></ul><ul><li>No name conflict with name of definition or name of element or attribute </li></ul><ul><li>Similar to <complexType> in XML Schema </li></ul>
  • 24. Definition Examples <ul><li>XML syntax (see def.rng ): </li></ul><ul><li><define name=&quot;date&quot;> <element name=&quot;date&quot;> <element name=&quot;year&quot;><text/></element> <element name=&quot;month&quot;><text/></element> <element name=&quot;day&quot;><text/></element> </element> </define> </li></ul><ul><li>Compact syntax (see def.rnc ): </li></ul><ul><li>date = element date {element year {text}, element month {text}, element day {text} } </li></ul>
  • 25. Grammar <ul><li>If <define> elements are used, both <grammar> and <start> must be used as well </li></ul><ul><li><grammar> becomes root element for schema </li></ul><ul><li><start> indicates document element in the instance (similar to what DOCTYPE does) </li></ul>
  • 26. grammar, start & ref Example <ul><li>XML syntax ( def.rng/.rnc with def.xml ): </li></ul><ul><li><grammar xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> <start> <ref name=&quot;date&quot;/> </start> <define name=&quot;date&quot;> <element name=&quot;date&quot;> <element name=&quot;year&quot;><text/></element> <element name=&quot;month&quot;><text/></element> <element name=&quot;day&quot;><text/></element> </element> </define> </grammar> </li></ul>
  • 27. Datatypes <ul><li>RELAX NG supports external datatype libraries, namely XML Schema datatypes </li></ul><ul><li>The datatypeLibrary attribute indicates the namespace for the datatype library (inherited) </li></ul><ul><li>The <data> element with the type attribute </li></ul><ul><li><param> , child element of <data> , indicates facets of datatype per XML Schema </li></ul>
  • 28. Datatypes in Compact Syntax <ul><li>The datatype library is automatically declared for XML Schema datatypes </li></ul><ul><li>xsd prefix is required unless XML Schema datatypes is redeclared </li></ul><ul><li>parameters are defined with literal strings </li></ul>
  • 29. Datatype Examples <ul><li>XML syntax ( year.rng with year.xml ): </li></ul><ul><li><element name=&quot;year&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> datatypeLibrary=&quot;http://www.w3.org/2001/XMLSchema-datatypes&quot;> <data type=&quot;gYear&quot;> </li></ul><ul><li><param name=&quot;minInclusive&quot;>2002</param> </li></ul><ul><li><param name=&quot;maxInclusive&quot;>2005</param> </li></ul><ul><li></data> </element> </li></ul><ul><li>Compact syntax ( year.rnc also with year.xml ): </li></ul><ul><li>element year { </li></ul><ul><li>xsd:gYear {minInclusive=&quot;2002&quot; maxInclusive=&quot;2005&quot;} </li></ul><ul><li>} </li></ul>
  • 30. Enumerations <ul><li>In DTDs, enumerations are only possible in attributes, defined in a DTD like this: </li></ul><ul><li><!ATTLIST week day (m|w|f) #REQUIRED> </li></ul><ul><li>Enumerations possible in RELAX NG in both elements and attributes </li></ul><ul><li>RELAX NG uses the <value> element in XML syntax, literals separated by | in compact syntax </li></ul><ul><li>XML Schema uses the <xs:enumeration> facet element, which is a child of <xs:restriction> , which is a child of <xs:simpleType> </li></ul>
  • 31. Enumeration Examples <ul><li>XML syntax ( day.rng with day.xml ): </li></ul><ul><li><element name=&quot;day&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> <choice> <value>m</value> <value>w</value> <value>f</value> </choice> </element> </li></ul><ul><li>Compact syntax ( day.rnc also with day.xml ): </li></ul><ul><li>element day { &quot;m&quot; | &quot;w&quot; | &quot;f&quot; } </li></ul>
  • 32. Lists <ul><li>A list is whitespace-separated sequence of tokens </li></ul><ul><li>RELAX NG uses the <list> element to define a list, followed by one or more <data> elements </li></ul><ul><li>Compact syntax uses the list keyword followed by a comma separated list of types </li></ul>
  • 33. More on Lists <ul><li>Can use occurrence constraints such as <optional> or ? , <oneOrMore> or + , <zeroOrMore> or * </li></ul><ul><li>DTDs use NMTOKENS , IDREFS , and ENTITIES , but for attributes only </li></ul><ul><li>XML Schema uses <xs:list> , a child of <xs:simpleType> , which can be constrained by facets </li></ul>
  • 34. List Examples <ul><li>XML syntax ( vertex.rng with vertex.xml ): </li></ul><ul><li><element name=&quot;vertex&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot; </li></ul><ul><li>datatypeLibrary=&quot;http://www.w3.org/2001/XMLSchema-datatypes&quot;> </li></ul><ul><li><list> </li></ul><ul><li><data type=&quot;float&quot;/> </li></ul><ul><li><data type=&quot;float&quot;/> </li></ul><ul><li><data type=&quot;float&quot;/> </li></ul><ul><li></list> </li></ul><ul><li></element> </li></ul><ul><li>Compact syntax ( vertex.rnc also with vertex.xml ): </li></ul><ul><li>element vertex {list {xsd:float, xsd:float, xsd:float}} </li></ul>
  • 35. Interleave <ul><li>In RELAX NG, you can define a pattern where element may appear in any order with <interleave> </li></ul><ul><li>Restores SGML’s & connector (both in either order) </li></ul><ul><li>Uses a non-deterministic content model, which DTDs and XML Schema forbid </li></ul><ul><li>Can come close with ANY in DTDs and <xs:all> or <xs:choice> in XML Schema </li></ul><ul><li>Can use occurrence constraints in <interleave> </li></ul>
  • 36. Interleave Examples <ul><li>XML syntax ( name.rng with name.xml ) </li></ul><ul><li><element name=&quot;name&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> </li></ul><ul><li><interleave> </li></ul><ul><li><element name=&quot;family&quot;><text/></element> </li></ul><ul><li><oneOrMore> </li></ul><ul><li><element name=&quot;given&quot;><text/></element> </li></ul><ul><li></oneOrMore> </li></ul><ul><li></interleave> </li></ul><ul><li></element> </li></ul><ul><li>Compact syntax ( name.rnc with name.xml ): </li></ul><ul><li>element name { element family {text} </li></ul><ul><li>& element given{text}+ } </li></ul>
  • 37. Mixed Content Model <ul><li>Mixed content models allow child elements to appear in any order and to be mixed with text </li></ul><ul><li>In DTDs, the model is (see mixed.dtd ): </li></ul><ul><li><!ELEMENT name (#PCDATA | family | given)*> </li></ul><ul><li>RELAX NG XML syntax uses <mixed> element </li></ul>
  • 38. More on Mixed Content <ul><li><mixed> is syntax sugar for <interleave> with <text/> </li></ul><ul><li>Compact syntax uses mixed keyword, comma separated patterns </li></ul><ul><li>XML Schema uses the boolean attribute mixed on either <xs:complexType> or <xs:complexContent> </li></ul>
  • 39. Mixed Examples <ul><li>XML syntax ( mixed.rng with instance mixed.xml ): </li></ul><ul><li><element name=&quot;name&quot; xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot;> <mixed> <element name=&quot;family&quot;><text/></element> <element name=&quot;given&quot;><text/></element> </mixed> </element> </li></ul><ul><li>Compact syntax ( mixed.rnc with instance mixed.xml ): </li></ul><ul><li>element name {mixed { element family {text}, element given{text}} } </li></ul>
  • 40. Grammars <ul><li>The <externalRef> element references an external pattern via a required href attribute </li></ul><ul><li>You can combine grammars using the combine attribute on define with a value of interleave or &= (match in any order) or choice or |= (match one of any) </li></ul><ul><li>The <include> element merges grammar together (has an href attribute) </li></ul>
  • 41. More on Grammars <ul><li>You can nest a grammar in an <include> element which will override a definition with the same name </li></ul><ul><li>You can also use <notAllowed/> when merging grammars, forcing you to redefine not-allowed grammars </li></ul><ul><li>Grammars can be nested; <parentRef> references or escapes to the parent grammar in a nested grammar, allowing you to redefine a child grammar using a parent, a sort of import </li></ul>
  • 42. Name Classes <ul><li>Name classes allow you to include or exclude whole classes of names in a pattern </li></ul><ul><li><name> allows a given name </li></ul><ul><li><anyName/> allows any name </li></ul><ul><li><nsName> allows names from a given namespace </li></ul><ul><li>The <except> element removes names from a class </li></ul>
  • 43. Schematron & RELAX NG <ul><li>Schematron is an assertion-based, rather than a grammar-based schema language </li></ul><ul><li>Uses path expressions (XSLT and XPath) </li></ul><ul><li>The “feather duster” that can reach corners of your instances, where grammars can’t (Rick Jelliffe) </li></ul><ul><li>Co-occurrence constraints possible </li></ul><ul><li>Can embed Schematron in RELAX NG </li></ul>
  • 44. Annotations <ul><li>You can use elements and attributes from other vocabularies in RELAX NG, such as XHTML </li></ul><ul><li><a:documentation> is a special documentation element defined in the RELAX NG DTD compatibility spec (http://www.oasis-open.org/committees/relax-ng/compatibility.html) </li></ul><ul><li><div> allows a place to add foreign attributes for documentation purposes </li></ul>
  • 45. RELAX NG Resources <ul><li>RELAX NG: http://www.relaxng.org </li></ul><ul><li>OASIS: http://www.oasis-open.org </li></ul><ul><li>RELAX: http://www.xml.gr.jp/relax/ </li></ul><ul><li>James Clark, TREX, Jing, Trang: http://www.thaiopensource.com </li></ul><ul><li>Design of RELAX NG: http://www.thaiopensource.com/relaxng/ design.html </li></ul>
  • 46. Conclusion Thanks for your time! Comments or questions? [email_address]

×