Home arrow XML arrow Page 4 - Datatypes and More in RELAX NG
XML

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
TABLE OF CONTENTS:
  1. · Datatypes and More in RELAX NG
  2. · Using the grammar element
  3. · Creating named patterns
  4. · Creating named patterns, continued

print this article
SEARCH DEVARTICLES

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:

<define name="cityState">
    <group>
        <element name="city">
            <text/>
        </element>
        <element name="state">
            <text/>
        </element>
    </group>
</define>

And the modified location pattern in the XML schema would look like this:

<define name="location">
    <choice>
        <element name="zipCode">
            <text/>
        </element>
        <ref name="cityState"/>
    </choice>
</define>

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.

blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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