XML
  Home arrow XML arrow Providing Options in RELAX NG
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Providing Options in RELAX NG
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-08-04

    Table of Contents:
  • Providing Options in RELAX NG
  • Giving the user choices, continued
  • Enumerations with the value element
  • Element order and interleaving

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Providing Options in RELAX NG


    (Page 1 of 4 )

    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.

    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>

    More XML Articles
    More By Peyton McCullough


       · Hello, all,This is another article in my short series on the RELAX NG schema...
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT