Home arrow XML arrow Datatypes and More in RELAX NG

Datatypes and More in RELAX NG

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.

Author Info:
By: Peyton McCullough
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
August 11, 2008
  1. · Datatypes and More in RELAX NG
  2. · Using the grammar element
  3. · Creating named patterns
  4. · Creating named patterns, continued

print this article

Datatypes and More in RELAX NG
(Page 1 of 4 )

Datatypes in RELAX NG

In the last two articles, if an element did not contain child elements, then it was either empty or it contained plain text. This works fine for some situations. For example, there's no other practical way to represent cities and states. However, in other situations, the text pattern just doesn't work. For example, the date attribute accepts any sort of text. We specified a date in the form YYYY-MM-DD, like this:

<person date="2008-06-30">

However, there's nothing to stop a person from putting the date in the form MM-DD-YYYY:

<person date="06-30-2008">

Nor is there anything to stop a user from doing something like this:

<person date="a week or so ago">

The document will still validate. Clearly, the text pattern is far too loose for a large number of situations, such as when representing a date. Thankfully, however, RELAX NG supports datatypes to remedy this situation. Instead of using the text pattern, it's possible to use stricter datatypes. RELAX NG itself actually only defines two datatypes, string and token. The string datatype takes strings as-is for comparison, without reducing whitespace, while the token datatype reduces whitespace.

These two datatypes will only get you so far, though, and we still can't represent our date properly. However, in most implementations of RELAX NG, you can actually use XML Schema datatypes. This ability provides access to a number of datatypes beyond just string and token, and it will allow us, at the very least, to represent the date properly.

The datatypes first need to be specified as a datatype library. This way, RELAX NG knows what datatypes it is working with. Using the XML syntax, this is done by setting the datatypeLibrary attribute. The value of this attribute takes the form of a URI. In the case of the XML Schema datatypes, this URI is http://www.w3.org/2001/XMLSchema-datatypes.

Every element in the schema has this attribute, and if set, the element and any of its children will use the corresponding datatype library, unless its children specify something different. So, the value of the attribute is actually inherited. We can go ahead and set this attribute on the root element of the XML schema:

<element name="people" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" xmlns="http://relaxng.org/ns/structure/1.0">

Using the compact syntax, datatype libraries are identified using a prefix. The xsd prefix should already be set up for you, but if you wanted to set a prefix up yourself, you'd insert a line like this at the top of the schema definition:

datatypes xsd = "http://www.w3.org/2001/XMLSchema-datatypes"

Now the datatypes are ready to be used. To make use of datatypes using the XML syntax, the data element is used. This element will match the datatype specified by the type attribute. To represent the value of the person element's date attribute properly, we'd use the date datatype, which calls for a date in the form YYYY-MM-DD. Using the data element, the date attribute would be set up in the schema like this:

<attribute name="date">
    <data type="date"/>

Using the compact syntax, the process is even simpler. In the compact syntax, the datatype is specified inside of an element's or attribute's brackets. So, only one word has to be changed in the compact schema:

attribute date { xsd:date }

Now something like this won't validate:

<person date="a week or so ago">

But a proper date will:

<person date="2008-06-30">

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials