XML
  Home arrow XML arrow Page 3 - An Introduction to 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

An Introduction to RELAX NG
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-07-28

    Table of Contents:
  • An Introduction to RELAX NG
  • Getting started with elements
  • Adding child elements
  • Adding attributes

  • 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


    An Introduction to RELAX NG - Adding child elements


    (Page 3 of 4 )

    Of course, if we're to follow the scenario of a list of people mentioned earlier, then the people element will need to contain person elements, not text. In order to make room for a child element in the schema, we simply need to place one element within the parent. So, in the XML schema, the text element needs to be replaced with another element element. Just as before, this element also needs to have some sort of child. For now, we'll just make it empty:

    <element name="people"  xmlns="http://relaxng.org/ns/structure/1.0">
        <element name="person">
            <empty/>
        </element>
    </element>

    The process for the compact schema is very simple. The child element simply needs to be placed within the parent element:

    element people {
        element person { empty }
    }

    Now, in the instance document, we're able to place a person element within the people element:

    <people>
        <person/>
    </people>

    However, there's one problem: in the schema, we've provided for one element. If we're to create a list of people, though, we need to provide for multiple elements within the schema. This isn't very hard, but there are two ways we can do this. We can either allow one or more person elements, or we can allow zero or more person elements. So, the choice is really between allowing the people element to be empty or not. Let's start by allowing one or more person elements. In the XML schema, this is done by placing the element's definition within a oneOrMore element:

    <element name="people" xmlns="http://relaxng.org/ns/structure/1.0">
        <oneOrMore>
            <element name="person">
                <empty/>
            </element>
        </oneOrMore>
    </element>

    In the compact schema, the process is even simpler. The compact syntax uses quantifiers like the ones found in regular expressions. The quantifiers are placed after the element to be quantified. So, in order to specify that one or more of an element is to appear, an addition symbol (“+”) is added after the element's definition:

    element people {
        element person { empty }+
    }

    Multiple person elements can now appear in the document, as long as there is at least one:

    <people>
        <person/>
        <person/>
    </people>

    The process for allowing zero or more elements to appear is very similar. For the XML schema, the zeroOrMore element is used:

    <element name="people" xmlns="http://relaxng.org/ns/structure/1.0">
        <zeroOrMore>
            <element name="person">
                <empty/>
            </element>
        </zeroOrMore>
    </element>

    And for the compact schema, an asterisk (“*”) is added after the element's definition:

    element people {
        element person { empty }*
    }

    Now the document will validate even if the people element is empty:

    <people>
    </people>

    More XML Articles
    More By Peyton McCullough


       · Hello, all,This is the first of three articles introducing 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
    Stay green...Green IT