XML
  Home arrow XML arrow Page 3 - 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  
Mobile Linux 
App Generation ROI 
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 - 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">
        <choice>
            <value>male</value>
            <value>female</value>
        </choice>
    </element>

    (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:

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

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

    <person>
        <zipCode>29555</zipCode>
        <gender>unknown</gender>
    </person>

    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.

    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 2 hosted by Hostway
    Stay green...Green IT