Home arrow XML arrow Page 2 - 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 - Giving the user choices, continued
(Page 2 of 4 )

It's time to go ahead and give the person element some child elements. One piece of information that we might want an instance document to represent is the person's location. But how do we represent a person's location? One way to do this would be to store the person's zip code. Another way to do this would be to store the person's city and state. We could choose one of these ways and incorporate it into the schema, but there's another option out there.

We could have the author of the document choose one method or the other. This is entirely possible. Just as it's possible to make one element optional, it's possible to allow a choice between one element and another.

So, we need to offer the user a choice between declaring a zipCode element, which represents the person's zip code, or declaring a city element and a state element, which represent the person's city and state.

In the XML schema, this is done using the choice element. Each of the choice element's children represents a choice for the user. The user will have to select only one. However, it's possible to create a choice with multiple elements by using the group element, which groups its children together. This way, the city and state elements can be grouped together as a single choice.

With this in mind, the choice element needs to have two children. One needs to be an element representing the zipCode element, and the other needs to be a group element with elements representing the city and state elements. The additions to the XML schema (made in the element representing person, of course) would look like this:

<choice>
    <element name="zipCode">
        <text/>
    </element>
    <group>
        <element name="city">
            <text/>
        </element>
        <element name="state">
            <text/>
        </element>
    </group>
</choice>

Using the compact syntax, choices are expressed differently. In the compact syntax, choices are made using the pipe symbol (|) which functions and looks just like a logical OR operator in some programming languages. The entire choice section is placed in a set of parentheses, and elements are also grouped using parentheses. This may sound a bit complicated at first, but it really isn't. The resulting compact schema would look like this:

element people {
    element person {
        attribute date { text }?,
        (element zipCode { text }
         | (element city { text },
             element state { text }))
    }*
}

The following instance document will now compile. It contains two person elements, one which specifies location using the zipCode element, and one which specifies location using city and state elements:

<people>
    <person date="2008-06-30">
        <city>Topeka</city>
        <state>Kansas</state>
    </person>
    <person>
        <zipCode>29555</zipCode>
    </person>
</people>

In the above example, we gave the user a choice between elements. However, it's also possible to give the user a choice between attributes, or it's possible to mix attributes and elements together in the same choice.

Interestingly enough, the optional element is actually converted into a choice element by your implementation. One of the choices is the optional element or attribute, and the other choice is simply an empty element. So, when we made the date attribute optional, we could have actually done this:

<choice>
    <attribute name="date"/>
    <empty/>
</choice>

The exact same result would have been achieved.


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