Home arrow XML arrow Page 2 - 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 - Using the grammar element
(Page 2 of 4 )

You may remember from the very first article that I said a RELAX NG implementation will actually transform a schema a bit, and the element element will no longer be the root element. The element that replaces it as the root element is the grammar element. Within this element is a single start element, which houses the main pattern.

We can easily modify the XML schema to use the grammar element. The existing schema simply needs to be put inside of a start element, which then needs to be put inside of a grammar element. After doing this, the entire XML schema would look like this:

<grammar xmlns="http://relaxng.org/ns/structure/1.0">
        <element name="people" datatypeLibrary=
                <element name="person">
                        <attribute name="date">
                            <data type="date"/>
                            <element name="zipCode">
                                <element name="city">
                                <element name="state">
                        <element name="gender">
                                <value type="string">male</value>
                                <value type="string">female</value>

Notice how the namespace declaration has been moved to the grammar element.

Using the compact syntax, the process is slightly different. In the compact syntax, there is a grammar keyword. After it are placed brackets, and the word start followed by an equal sign is placed within the brackets. The main pattern is then “assigned” to start. The resulting compact schema looks like this:

grammar {
    start =
        element people {
            element person {
                attribute date { xsd:date }?,
                    (element zipCode { xsd:string }
                     | (element city { text },
                         element state { text })) &
                    element gender { "male" | "female" }

An instance document will still validate exactly as before.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials