RELAX NG What is RELAX NG n RELAX

  • Slides: 31
Download presentation
RELAX NG

RELAX NG

What is RELAX NG? n RELAX NG is a schema language for XML n

What is RELAX NG? n RELAX NG is a schema language for XML n n OASIS is the Organization for the Advancement of Structured Information Standards n n n It is an alternative to DTDs and XML Schemas It is based on earlier schema languages, RELAX and TREX It is not a W 3 C standard, but is an OASIS standard eb. XML (Enterprise Business XML) is a joint effort of OASIS and UN/CEFACT (United Nations Centre for Trade Facilitation and Electronic Business) OASIS developed the highly popular Doc. Book DTD for describing books, articles, and technical documents RELAX NG has recently been adopted as an ISO/IEC standard 2

Design goals n n Simple and easy to learn Uses XML syntax n n

Design goals n n Simple and easy to learn Uses XML syntax n n Does not change the information set of an XML document n n n n But there is also a “concise” (non-XML) syntax (I’m not sure what this means) Supports XML namespaces Treats attributes uniformly with elements so far as possible Has unrestricted support for unordered content Has unrestricted support for mixed content Has a solid theoretical basis Can make use of a separate datatyping language (such W 3 C XML Schema Datatypes) 3

RELAX NG tools n n Assorted validators and translators are now available for RELAX

RELAX NG tools n n Assorted validators and translators are now available for RELAX NG See http: //relaxng. org 4

Basic structure n A RELAX NG specification is written in XML, so it obeys

Basic structure n A RELAX NG specification is written in XML, so it obeys all XML rules n n n The RELAX NG specification has one root element The document it describes also has one root element The root element of the specification is element If the root element of your document is book, then the RELAX NG specifications begins: <element name="book" xmlns="http: //relaxng. org/ns/structure/1. 0"> and ends: </element> 5

Data elements n RELAX NG makes a clear separation between: n n n For

Data elements n RELAX NG makes a clear separation between: n n n For starters, we will use the two (XML-defined) elements: n n n the structure of a document (which it describes) the datatypes used in the document (which it gets from somewhere else, such as from XML Schemas) <text>. . . </text> (usually written <text/>) n Plain character data, not containing other elements <empty></empty> (usually written <empty/>) n Does not contain anything Other datatypes, such as <double>. . . </double> are not defined in RELAX NG n To inherit datatypes from XML Schemas, use: datatype. Library="http: //www. w 3. org/2001/XMLSchema-datatypes" as an attribute of the root element 6

Data types from XML Schemas n Here are some of the predefined numeric types:

Data types from XML Schemas n Here are some of the predefined numeric types: xs: decimal xs: byte xs: short xs: int xs: long xs: positive. Integer xs: negative. Integer xs: non. Positive. Integer xs: non. Negative. Integer 7

Predefined date and time types n n n xs: date -- A date in

Predefined date and time types n n n xs: date -- A date in the format CCYY-MM-DD, for example, 2002 -11 -05 xs: time -- A date in the format hh: mm: ss (hours, minutes, seconds) xs: date. Time -- Format is CCYY-MM-DDThh: mm: ss n n The T is part of the syntax Allowable restrictions on dates and times: n enumeration, min. Inclusive, min. Exclusive, max. Inclusive, max. Exclusive, pattern, white. Space 8

Defining tags n n To define a tag (and specify its content), use <element

Defining tags n n To define a tag (and specify its content), use <element name="my. Element"> <!-- Content goes here --> </element> Example: The DTD <!ELEMENT name (first. Name, last. Name)> <!ELEMENT first. Name (#PCDATA)> <!ELEMENT last. Name (#PCDATA)> Translates to: <element name="name"> <element name="first. Name"> <text/> </element> <element name="last. Name"> <text/> </element> Note: As in the DTD, the components must occur in order 9

RELAX NG describes patterns n n Your RELAX NG document specifies a pattern that

RELAX NG describes patterns n n Your RELAX NG document specifies a pattern that matches your valid XML documents For example, the pattern: <element name="name"> <element name="first. Name"> <text/> </element> <element name="last. Name"> <text/> </element> n Will match the XML: <name> <first. Name>David</first. Name> <last. Name>Matuszek</last. Name> </name> 10

Easy tags <zero. Or. More>. . . </zero. Or. More> The enclosed content occurs

Easy tags <zero. Or. More>. . . </zero. Or. More> The enclosed content occurs zero or more times <one. Or. More>. . . </one. Or. More> The enclosed content occurs one or more times <optional>. . . </optional> The enclosed content occurs once or not at all <choice>. . . </choice> Any one of the enclosed elements may occur <!-- This is an XML comment; it is not a container, ��� and it may not contain two consecutive hyphens --> 11

Example <element name="address. List"> <zero. Or. More> <element name="name"> <element name="first. Name"> <text/> </element>

Example <element name="address. List"> <zero. Or. More> <element name="name"> <element name="first. Name"> <text/> </element> <element name="last. Name"> <text/> </element> <element name="address"> <choice> <element name="email> <text/> </element> <element name="USPost"> <text/> </element> </choice> </element> </zero. Or. More> </element> 12

Enumerations n The <value>. . . </value> pattern matches a specified value n n

Enumerations n The <value>. . . </value> pattern matches a specified value n n Example: <element name="gender"> <choice> <value>male</value> <value>female</value> </choice> </element> The contents of <value> are subject to whitespace normalization: n n Leading and trailing whitespace is removed Internal sequences of whitespace characters are collapsed to a single blank 13

More about data n To inherit datatypes from XML Schemas, add this attribute to

More about data n To inherit datatypes from XML Schemas, add this attribute to the root element: datatype. Library = "http: //www. w 3. org/2001/XMLSchema-datatypes" n You can access the inherited types with the <data> tag, for instance, <data type="double> n n n The <data> pattern must match the entire content of the enclosing tag, not just part of it <element name="illegal. Use"> <!-- Don't do this! --> <data type="double"/> <element name="more. Stuff"> <text/> </element> If you don't specify a datatype library, RELAX NG defines the following for you (along with <text/> and <empty/>): n n <string/> : No whitespace normalization is done <token/> : A sequence of characters containing no whitespace 14

<group> n n <group>. . . </group> is used as “fat parentheses” Example: <choice>

<group> n n <group>. . . </group> is used as “fat parentheses” Example: <choice> <element name="name"> <text/> <element> choice #1 <group> <element name="first. Name"> <text/> </element> choice #2 <element name="last. Name"> <text/> </element> </group> </choice> n 15

Attributes n Attributes are defined practically the same way as elements: n n Example:

Attributes n Attributes are defined practically the same way as elements: n n Example: n n <attribute name="attribute. Name">. . . </attribute> <element name="name"> <attribute name="title"> <text/> </attribute> <element name="first. Name"> <text/> </element> <element name="last. Name"> <text/> </element> Matches: n <name title="Dr. "> <first. Name>David</first. Name> <last. Name>Matuszek</last. Name> </name> 16

More about attributes n n n With attributes, as with elements, you can use

More about attributes n n n With attributes, as with elements, you can use <optional>, <choice>, and <group> It doesn’t make sense to use <one. Or. More> or <zero. Or. More> with attributes In keeping with the usual XML rules, n n The order in which you list elements is significant The order in which you list attributes is not significant 17

Still more about attributes n n <attribute name="attribute. Name"> <text/> </attribute> can be (and

Still more about attributes n n <attribute name="attribute. Name"> <text/> </attribute> can be (and usually is) abbreviated as <attribute name="attribute. Name"/> However, <element name="element. Name"> <text/> </element> can not be abbreviated as <element name="element. Name"/> n If an element has no attributes and no content, you must use <empty/> explicitly 18

<list> n <list> pattern </list> matches a whitespaceseparated list of tokens, and applies the

<list> n <list> pattern </list> matches a whitespaceseparated list of tokens, and applies the pattern to those tokens n Example: <!-- A floating-point number and some integers --> <element name="vector"> <list> <data type="float"/> <one. Or. More> <data type="int"/> </one. Or. More> </list> </element> 19

<interleave> n n <interleave>. . . </interleave> allows the contained elements to occur in

<interleave> n n <interleave>. . . </interleave> allows the contained elements to occur in any order <interleave> is more sophisticated than you might expect n If a contained element can occur more than once, the various instances do not need to occur together 20

Interleave example <element name="contact. Information"> <interleave> <zero. Or. More> <element name="phone"> <text/> </element> </zero.

Interleave example <element name="contact. Information"> <interleave> <zero. Or. More> <element name="phone"> <text/> </element> </zero. Or. More> <one. Or. More> <element name="email"> <text/> </element> </one. Or. More> </interleave> </element> <contact. Information> <email>dave@acm. org</email> <phone>215 -898 -8122</phone> <email>davebiz@matuszek. org</email> </contact. Information> 21

<mixed> n n <mixed> allows mixed content, that is, both text and patterns If

<mixed> n n <mixed> allows mixed content, that is, both text and patterns If pattern is a RELAX NG pattern, then <mixed> pattern </mixed> is shorthand for <interleave> <text/> pattern </interleave> 22

Example of <mixed> • Pattern: Without this we get one bold or one italic

Example of <mixed> • Pattern: Without this we get one bold or one italic <element name="words"> <mixed> <zero. Or. More> <choice> <element name="bold"> <text/> </element> <element name="italic"> <text/> </element> </choice> </zero. Or. More> </mixed> </element> n Matches: <words>This is <italic>not</italic> a <bold>great</bold> example, <italic>but</italic> it should suffice. </words> 23

The need for named patterns n So far, we have defined elements exactly at

The need for named patterns n So far, we have defined elements exactly at the point that they can be used n There is no equivalent of: n n With the RELAX NG we have discussed so far, each time we want to include a person, we would need to explicitly define both person and name at that point: n n <!ELEMENT person (name)> <!ELEMENT name (first. Name, last. Name)>. . . use person several places in the DTD. . . <element name="person"> <element name="first. Name"> <text/> </element> <element name="last. Name"> <text/> </element> The <grammar> element solves this problem 24

Syntax of <grammar> <grammar xmlns="http: //relaxng. org/ns/structure/1. 0"> <start>. . . usual RELAX NG

Syntax of <grammar> <grammar xmlns="http: //relaxng. org/ns/structure/1. 0"> <start>. . . usual RELAX NG elements, which may include: <ref name="Defined. Name"/> </start> <!-- One or more of the following: --> <define name="Defined. Name">. . . usual RELAX NG elements, attributes, groups, etc. </define> </grammar> 25

Use of <grammar> n To write a <grammar>, n Make <grammar> the root element

Use of <grammar> n To write a <grammar>, n Make <grammar> the root element of your specification n n Hence it should say xmlns="http: //relaxng. org/ns/structure/1. 0" Use, as the <start> element, a pattern that matches the entire (valid) XML document In each <define> element, write a pattern that you want to use other places in the specification Wherever you want to use a defined element, put <ref name="Name. Of. Defined. Element"> Note that defined elements may be used in definitions, not just in the <start> element n Definitions may even be recursive, but n Recursive references must be in an element, not an attribute 26

Long example of <grammar> n n <!ELEMENT name (first. Name, last. Name)> <grammar xmlns="http:

Long example of <grammar> n n <!ELEMENT name (first. Name, last. Name)> <grammar xmlns="http: //relaxng. org/ns/structure/1. 0"> <start> <ref name="Name"/> </start> <define name="Name"> <element name="name"> <element name="first. Name"> <text/> </element> <element name="last. Name"> <ref name="Last. Name"> XML is case sensitive-</element> Note that defined terms are </define> capitalized differently <define name="Last. Name"> <element name="last. Name"> <text/> </element> </define> </grammar> 27

Common usage I n n A typical way to use RELAX NG is to

Common usage I n n A typical way to use RELAX NG is to use a <grammar> with just the root element in <start> and every element described by a <define> <grammar xmlns="http: //relaxng. org/ns/structure/1. 0"> <start> <ref name="NOVEL"> </start> <define name="NOVEL"> <element name="novel"> <ref name="TITLE"/> <ref name="AUTHOR"/> <one. Or. More> <ref name="CHAPTER"/> </one. Or. More> </element> </define>. . . more. . . 28

Common usage II <define name="TITLE"> <element name="title"> <text/> </element> </define> <define name="AUTHOR"> <element name="author">

Common usage II <define name="TITLE"> <element name="title"> <text/> </element> </define> <define name="AUTHOR"> <element name="author"> <text/> </element> </define> <define name="CHAPTER"> <element name="chapter"> <one. Or. More> <ref name="PARAGRAPH"/> </one. Or. More> </element> </define> <define name="PARAGRAPH"> <element name="paragraph"> <text/> </element> </define> </grammar> 29

Replacing DTDs n With <grammar> and multiple <define>s, we can do essentially the same

Replacing DTDs n With <grammar> and multiple <define>s, we can do essentially the same things as a DTD n Advantages: n n RELAX NG is more expressive than a DTD; we can interleave elements, specify data types, allow specific data values, use namespaces, and control the mixing of data and patterns RELAX NG is written in XML RELAX NG is relatively easy to understand Disadvantages n n RELAX NG is extremely verbose n But there is a “compact syntax” that is much shorter RELAX NG is not (yet) nearly as well known n Hence there are fewer tools to work with it n This situation seems to be changing 30

The End So by this maxim be impressed, USE THE TOOLS THAT WORK THE

The End So by this maxim be impressed, USE THE TOOLS THAT WORK THE BEST. Do not yield your sovereign judgment, To any sort of political fudgement. The criterion of sound design Should be, must be, your guideline. And if you're designing documents, Try RNG. We charge no rents. -- John Cowan 31