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

       · 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek