XML
  Home arrow XML arrow Page 2 - 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 
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 - 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.

    More XML Articles
    More By Peyton McCullough


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

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT