Home arrow XML arrow Providing Options in RELAX NG
XML

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Providing Options in RELAX NG
(Page 1 of 4 )

Giving the user choices

In the last article, we created a schema requiring a root element named people that represents a list of people, with child elements named person to represent the individual people in the list. These are reasonable constraints, and we'll continue modifying the schema using the exact same scenario.

We also created an attribute of the person element named date, which represents the date on which the person was added to the list. However, is it really necessary to know on what date the person was added? Perhaps not. We can make the attribute optional, then. That way, the user can choose whether or not to give it some sort of value.

Making an attribute optional in RELAX NG is easy. In the XML schema, it's accomplished using the optional element. A child of the optional element does not have to appear in an instance document. In order to make the date attribute optional, then, we simply need to put its attribute element inside of an optional element. Here's how the schema would look with this modification:

<element name="people" xmlns="http://relaxng.org/ns/structure/1.0">
    <zeroOrMore>
        <element name="person">
            <optional>
                <attribute name="date"/>
            </optional>
        </element>
    </zeroOrMore>
</element>

In the compact schema, this is done by inserting a question mark (?) after the attribute. This is similar to the way in which we, earlier, declared that there could be one or more and zero or more person elements. Here's how the modified compact schema looks:

element people {
    element person {
        attribute date { text }?
    }*
}

Now the instance document can leave out the date attribute and still validate. Below, we create an instance document with two person elements. One of these elements contains a date attribute, but the other does not:

<people>
    <person date="2008-06-20"/>
    <person/>
</people>

The document validates, as expected.

Just as it's possible to make an attribute optional, it's also possible to make an element optional. Suppose, for example, that the date attribute had been a date element instead, so that the instance document looked like this:

<people>
    <person>
        <date>2008-06-20</date>
    </person>
</people>

Making the date element optional follows the same process as making the date attribute optional. The XML schema would simply have to swap the attribute element out for an element element:

<optional>
    <element name="date">
        <text />
    </element>
</optional>

(Above, the schema has been trimmed a bit because it's starting to take up too much space).

And the compact schema would simply have to swap a single word:

element people {
    element person {
        element date { text }?
    }*
}

The following instance document will not validate. It contains two person elements, one which contains a date element and one which does not:

<people>
    <person>
        <date>2008-06-20</date>
    </person>
    <person/>
</people>


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