RELAX NGWhat is RELAX NG?Design goalsRELAX NG toolsBasic structureData elementsData types from XML SchemasPredefined date and time typesDefining tagsRELAX NG describes patternsEasy tagsExampleEnumerationsMore about data<group>AttributesMore about attributesStill more about attributes<list><interleave>Interleave example<mixed>Example of <mixed>The need for named patternsSyntax of <grammar>Use of <grammar>Long example of <grammar>Common usage ICommon usage IIReplacing DTDsThe EndJan 14, 2019RELAX NG2What is RELAX NG?RELAX NG is a schema language for XMLIt is an alternative to DTDs and XML SchemasIt is based on earlier schema languages, RELAX and TREXIt is not a W3C standard, but is an OASIS standard OASIS is the Organization for the Advancement of Structured Information StandardsebXML (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 DocBook DTD for describing books, articles, and technical documentsRELAX NG has recently been adopted as an ISO/IEC standard3Design goalsSimple and easy to learnUses XML syntaxBut there is also a “concise” (non-XML) syntaxDoes not change the information set of an XML document(I’m not sure what this means)Supports XML namespacesTreats attributes uniformly with elements so far as possibleHas unrestricted support for unordered contentHas unrestricted support for mixed contentHas a solid theoretical basisCan make use of a separate datatyping language (such W3C XML Schema Datatypes)4RELAX NG toolsAssorted validators and translators are now available for RELAX NGSee http://relaxng.org5Basic structureA RELAX NG specification is written in XML, so it obeys all XML rulesThe RELAX NG specification has one root elementThe document it describes also has one root elementThe root element of the specification is elementIf 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>6Data elementsRELAX NG makes a clear separation between: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)For starters, we will use the two (XML-defined) elements:<text> ... </text> (usually written <text/>)Plain character data, not containing other elements<empty></empty> (usually written <empty/>)Does not contain anythingOther datatypes, such as <double>...</double>are not defined in RELAX NGTo inherit datatypes from XML Schemas, use:datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"as an attribute of the root element7Data types from XML SchemasHere are some of the predefined numeric types:xs:decimal xs:positiveIntegerxs:byte xs:negativeIntegerxs:short xs:nonPositiveIntegerxs:int xs:nonNegativeIntegerxs:long8Predefined date and time typesxs:date -- A date in the format CCYY-MM-DD, for example, 2002-11-05xs:time -- A date in the format hh:mm:ss (hours, minutes, seconds)xs:dateTime -- Format is CCYY-MM-DDThh:mm:ssThe T is part of the syntaxAllowable restrictions on dates and times: enumeration, minInclusive, minExclusive, maxInclusive, maxExclusive, pattern, whiteSpace9Defining tagsTo define a tag (and specify its content), use<element name="myElement"><!-- Content goes here --></element>Example: The DTD<!ELEMENT name (firstName, lastName)><!ELEMENT firstName (#PCDATA)><!ELEMENT lastName (#PCDATA)>Translates to:<element name="name"> <element name="firstName"> <text/> </element> <element name="lastName"> <text/> </element></element>Note: As in the DTD, the components must occur in order10RELAX NG describes patternsYour RELAX NG document specifies a pattern that matches your valid XML documentsFor example, the pattern: <element name="name"> <element name="firstName"> <text/> </element> <element name="lastName"> <text/> </element></element>Will match the XML: <name> <firstName>David</firstName> <lastName>Matuszek</lastName></name>11Easy tags<zeroOrMore> ... </zeroOrMore>The enclosed content occurs zero or more times<oneOrMore> ... </oneOrMore>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 -->12Example <element name="addressList"> <zeroOrMore> <element name="name"> <element name="firstName"> <text/> </element> <element name="lastName"> <text/> </element> </element> <element name="address"> <choice> <element name="email> <text/> </element> <element name="USPost"> <text/> </element> </choice> </element> </zeroOrMore></element>13EnumerationsThe <value>...</value> pattern matches a specified valueExample:<element name="gender"> <choice> <value>male</value> <value>female</value> </choice></element>The contents of <value> are subject to whitespace normalization:Leading and trailing whitespace is removedInternal sequences of whitespace characters are collapsed to a single blank14More about dataTo inherit datatypes from XML Schemas, add this attribute to the root element:datatypeLibrary = "http://www.w3.org/2001/XMLSchema-datatypes"You can access the inherited types with the <data> tag, for instance, <data type="double>The <data> pattern must match the entire content of the enclosing tag, not just part of it<element name="illegalUse"> <!-- Don't do this! --> <data type="double"/> <element name="moreStuff"> <text/> </element></element>If you don't specify a datatype library, RELAX NG defines the following for you (along with <text/> and <empty/>):<string/> : No whitespace normalization is done<token/> : A sequence of characters containing no whitespace15<group><group>...</group> is used as “fat parentheses”Example:<choice> <element name="name"> <text/> <element> <group> <element name="firstName"> <text/> </element> <element name="lastName"> <text/> </element> </group></choice>choice #1choice #216AttributesAttributes are defined practically the same way as elements:<attribute name="attributeName">...</attribute>Example:<element
View Full Document