Comments
Patrick Collands wrote: collands (AT) gmail com I'd be very grateful for an invitation. Thank you.
Cloud Expo on Google News

SYS-CON.TV

2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Click For 2008 West
Event Webcasts
RELAX NG: The Power Is in the Patterns
RELAX NG: The Power Is in the Patterns

Schema languages are languages that allow you to specify the structure of XML instance documents. RELAX NG (see www.relaxng.org) is an XML schema language that is considered to be simple, yet powerful. This article gives an overview of an important concept of the RELAX NG schema language called patterns. The power of RELAX NG can be found in its patterns.

Schema languages also describe the allowed names of elements and attributes that are found in XML instance documents. And they allow you to specify element ordering, occurrence, and allowed content, like simple text, or datatypes, like integers. Some examples of schema languages are W3C XML Schema, RELAX NG, Schematron, and DTD.

RELAX NG differs from other schema languages in that it's built around the concept of patterns. To understand the power of RELAX NG, you must first understand the basic RELAX NG patterns and how they can be combined.

Let's begin by taking a look at the following XML instance document:

<document pages="1" year="2002" > <title>Patterns are fun <author>Tom Gaven</author> <para> This is the <b>first</b> paragraph. </para> <para> And this is the <b>second</b>. </para> <prints>5 60 45</prints> </document>
This XML instance document contains elements and text. To give you an example of the RELAX NG patterns found in this XML instance document, we could say (ignoring attributes) that the pattern for the document element would be element title, followed by element author, followed by one or more para elements. The pattern for the para element would be a mixture of text and element b. The title, author, and b elements all have a similar pattern, text.

Listing 1 is a complete RELAX NG schema document that shows this pattern syntax. The syntax used in the listing is the compact syntax for RELAX NG. This schema can be used to validate the instance document. Line 1 is a top-level declaration that specifies we're using XML Schema datatypes tied to the "xsd" prefix. The keyword "start" specifies the root element of the instance document. (Datatypes, start, element, attribute, text, and list are all keywords.) RELAX NG's compact syntax uses the same occurrence symbols as DTDs (+,?,*).

The RELAX NG patterns are found to the right of the equals (=) sign. Definition names are to the left. Note that patterns can be combined, like the pattern "pages, year, title, author, para+". The comma specifies an ordering of the patterns. You can also group (or nest) patterns using parentheses. The ( text | b )* pattern specifies zero or more sets of text or b elements.

Patterns
Element and attribute patterns

The first two patterns we'll discuss are the patterns for elements and attributes. Following is the (compact) syntax for the element and attribute patterns:

P1. element nameClass { pattern }
P2. attribute nameClass { pattern }

These two patterns are recursive in that they allow other nested patterns. A nameClass is another great feature of RELAX NG, but for now just think of the nameClass as simply the name of the element or attribute. In the schema document lines 3-10 all use the element or attribute pattern.

Datatype patterns
P3. datatypeName params?
P4. datatypeName? DatatypeValue
P5. "text"

Pattern P3 is used on lines 4 and 10 in the schema document. It allows you to specify the datatype allowed for the content of an element or attribute.

Pattern P4 is used on line 5. The text "2002" is the DatatypeValue field. This specifies that the year element must have the content "2002". Pattern P5 is the text pattern. The keyword "text" specifies that text content is allowed.

Ref (Reference) patterns
P6. ref
P7. "(" pattern ")"

Pattern P6 allows patterns to reference definitions, which in turn define other patterns. The ref pattern is used in the schema document in Listing 1 in lines 2, 3, and 8. For example, the "b" reference on the right side of line 8 references the definition (b) on the left side of line 9. Pattern P7 allows patterns to be nested inside parentheses (see line 8 in the schema).

List pattern
P8. list { pattern }

The list pattern allows lists of content (separated by white space) to be specified. Line 10 in the schema specifies that the prints element can contain a "list of integers."

More patterns
Overall, 14 different patterns are supported by RELAX NG, eight of which are discussed above. The other available patterns are mixed, empty, notAllowed, grammar, parent ref, and external ref. The first three allow more controls on content; the last three allow for file and grammar modularity.

Power in Combinations
Individually, RELAX NG's patterns are powerful, but you can also combine them to handle more complex validation scenarios.

Combined patterns
x = element x { ( list { xsd:integer } | b ) }
b = element b { text }

Element x contains either a list of integers or an element b.

x = element x { "1.0" | "NaN" | num }
num = element num { text }

Element x contains either the string "1.0", the string "NaN", or a num element.

x = element x { (a,b)|(a,c) }
a = element a { text }
b = attribute b { text }
c = attribute c { text }

Element x contains (element a and attribute b) OR (element a and attribute c).

RELAX NG patterns are quite powerful, yet easy to learn and use. With their ability to combine, they offer capability not found in either DTDs or XML Schema.

About Tom Gaven
Tom Gaven lives in northern Virginia, and has developed and delivered training on many different technologies. He has authored over 30 courses, including Assembler, C, C++, Java, OS/2, and Windows. He also authored MindQ's Developer Training for Java program. In the last 2 years, he has been architecting and developing products with XML, XSLT, XML Schema, RELAX NG, Java, and Schematron. Tom is currently working on tools and courseware to make XML easier to use. See http://www.xmldistilled.com for more information.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Latest Cloud Developer Stories
CloudBench Applications, Inc. announced its financial results for the three months and nine months ending September 30, 2009. All amounts are stated in Canadian dollars unless otherwise noted. Revenues from BasicGov, the Company's cloud computing solution for local government, gr...
The new contract is an industry first, with CSC being the first Microsoft partner to lead and win a cloud computing services agreement of this scale. Under terms of the contract, CSC will provide Royal Mail Group's 30,000 employees with access to new IT services using Microsoft's...
Operates in over 170 countries and is one of the world’s leading providers of communications solutions and services. Richard Tarboton talks for MeettheBoss.TV on his role as Head of Energy & Carbon for BT and what they are doing towards reducing carbon emissions.
CA is going to put its Agile Planner software on salesforce.com’s Force.com platform in the first half to accelerate development time and give users visibility over their development initiatives to reduce time-to-market. Customers are supposed to be able to accelerate the deploym...
Despite its uncertain fate Sun soldiers on. Monday it trotted out a cloud-based multiplatform desktop as a service for K-12 and community colleges that can run Windows, the Mac OS, Linux and Solaris applications to nearly any client device, including its own Sun Ray thin clients....
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE

Breaking Cloud Computing News
CloudBench Applications, Inc. announced its financial results for the three months and nine months e...