RELAX NGCaveatWhat is RELAX NG?Design goalsRELAX NG toolsBasic structureData elementsDefining 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 13, 2019RELAX NG2CaveatI did not have a RELAX NG validator when I wrote these slides.Therefore, if an example appears to be wrong, it probably is.3What 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 standard4Design 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)5RELAX NG toolsJingAn open source validator written in JavaSun’s MSVAnother validatorDTDinstTranslates from DTDs into RNG (RELAX NG) syntax or RNG “compact” syntaxTrangTranslates RNG compact syntax into RNG syntax Translates RNG or RNG compact syntax into DTDsSun’s RELAX NG ConverterTranslates DTDs into RNG syntax (but not well)Translates an XML Schema subset into RNG syntax (imperfectly)6Basic 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>7Data 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 element8Defining 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 order9RELAX 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>10Easy 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<!-- An XML comment - not a container, and may not contain two consecutive hyphens -->11Example <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>12EnumerationsThe <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 blank13More about dataRemember: To 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 whitespace14<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 #215AttributesAttributes are defined practically the same way as elements:<attribute name="attributeName">...</attribute>Example:<element name="name"> <attribute name="title"> <text/> </attribute> <element name="firstName"> <text/> </element> <element name="lastName"> <text/> </element></element>Matches:<name
View Full Document