Home arrow XML arrow Page 3 - Providing Options in RELAX NG

Providing Options in RELAX NG

XML schemas don't have to be rigid. Sometimes, it's best to provide flexibility and allow the author of XML documents to make choices. In this second part of a three-part article, we'll make some modifications to the schema we created in the first part, and learn how to make some things optional.

Author Info:
By: Peyton McCullough
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
August 04, 2008
  1. · Providing Options in RELAX NG
  2. · Giving the user choices, continued
  3. · Enumerations with the value element
  4. · Element order and interleaving

print this article

Providing Options in RELAX NG - Enumerations with the value element
(Page 3 of 4 )

In some situations, it's not an element or an attribute that a user should choose from, but, instead, a value. For example, consider a gender element. Valid values for this element consist of “male” and “female.” It is possible to limit the value of the element to one of these two values. In the XML schema, this can be done using the choice element in conjunction with the value element. Since there are two possible values, we need two value elements, one for “male” and one for “female.” The two value elements will be children of the choice element. The resulting XML schema will look like this:

<element name="gender">

(If you're following along on your own, make sure to place this at the end of the person element's declaration. The order of elements matters in RELAX NG. We'll get to this in a moment).

The process is even simpler using the compact syntax. Just as before, the pipe character is used, only this time the choice is between two values and not between two elements. Here's what the new compact schema looks like:

element people {
    element person {
        attribute date { text }?,
        (element zipCode { text }
         | (element city { text },
             element state { text })),
        element gender { "male" | "female" }

Now the following XML will validate because the values in the gender element are valid:

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

However, if we were to specify a value other than “male” or “female,” then the document would not validate:


The above modification would result in an invalid document.

Of course, this concept isn't restricted to elements. Attributes can also make use of this using the same process.

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