Home arrow XML arrow Page 4 - 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 - Element order and interleaving
(Page 4 of 4 )

So far, within the person element, the schema calls for one or two elements on location (depending on whether the user chooses to use the zip code for location or the city and state) and one element on gender. The order of these elements may not seem important, but, by default, they must appear in the instance document in the exact order that they appear in the schema definition. As the schema is applied, the patterns match the document in sequence.

Sometimes, this behavior is what you want, perhaps in order to keep the document organized. Other times, however, it really doesn't matter what order the elements are in, and the default behavior just gets in the way. Thankfully, for these situations, it's possible to throw order out of the picture.

Using the XML syntax, this is done using the interleave element. Patterns represented by child elements of the interleave element will match in any order. If we wanted to let the child elements of the person element be placed in any order, we'd simply wrap the corresponding patterns inside of an interleave element, like this:

<interleave>
    <choice>
        <element name="zipCode">
            <text/>
        </element>
        <group>
            <element name="city">
                <text/>
            </element>
            <element name="state">
                <text/>
            </element>
        </group>
    </choice>
    <element name="gender">
        <choice>
            <value>male</value>
            <value>female</value>
        </choice>
    </element>
</interleave>

Note, however, that the city and state still must appear in the same order since their patterns are not wrapped within an interleave element within their group. This behavior, however, is logical and is a good example of how order can contribute to organization and clarity.

Using the compact syntax, the process is even easier. Instead of separating the schema's patterns using commas, they must be separated using ampersands (&), which are called interleave connectors within RELAX NG:

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

Note how the interleaving part is contained within parentheses. It's actually not possible to combine the default behavior and interleaving within a single sequence. We could get rid of the comma after the attribute, replacing it with an ampersand. This would get rid of the need for the parentheses, but it would be silly since attributes are not affected by order. So, it's best to leave it as it is.

The following document will now validate:

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

Note how the the first gender element is above the location elements, while the second gender element is below the location element.

That's all we have time and room for now. Please check back next week for the conclusion to this article series.


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