Welcome to the third part of a three-part series on RELAX NG. In this part, we will discuss datatypes, the grammar element, and creating named patterns. That's a lot of ground to cover, so let's get started.
Datatypes and More in RELAX NG - Creating named patterns, continued (Page 4 of 4 )
Recall how the start element was represented in the compact schema. Its content was basically assigned to it, just as if it were a variable in a programming language. Named patterns are created in the same way. In fact, start itself is actually a special named pattern. In order to reference a named pattern elsewhere in the schema, you simply need to place the name of the pattern. The whole process is extraordinarily easy using the compact syntax. Here's the entire compact schema, with the location section made into a named pattern:
grammar { start = element people { element person { attribute date { xsd:date }?, location }* } location = ( (element zipCode { xsd:string } | (element city { text }, element state { text })) & element gender { "male" | "female" } ) }
It's also possible to reference a pattern within another pattern. Suppose we wanted to modify the location pattern, taking out the section with the city and state element declarations. These could be placed in a new pattern named “cityState” and referenced within the location pattern. The new pattern in the XML schema would look like this:
Using the compact syntax, the entire modified schema would look like this:
grammar { start = element people { element person { attribute date { xsd:date }?, location }* } location = ( (element zipCode { xsd:string } | cityState) & element gender { "male" | "female" } ) cityState = (element city { text }, element state {text }) }
By now, you've certainly noticed the amazing conciseness of the compact syntax. Whereas the XML schema has become too big to list in its entirety after each change in the schema, the compact schema is small enough to list each time.
Yet the choice of which syntax to use is entirely up to you, and with either syntax, you can make full use of RELAX NG's amazing functionality. Perhaps the most surprising feature of RELAX NG is that it manages to offer a complete schema solution while still being very simple and easy to use.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.