Home arrow XML arrow Page 3 - An Introduction to RELAX NG
XML

An Introduction to RELAX NG


RELAX NG is not a capitalized misspelling of something you probably get to do all too rarely as a busy programmer and web designer. If you use XML to any great degree, you'll want to take a close look at it. It can help make your life as a web developer easier, allowing you to relax a little more. Keep reading to learn what it is, and the advantages to using it. This is the first part of a three-part series.

Author Info:
By: Peyton McCullough
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
July 28, 2008
TABLE OF CONTENTS:
  1. · An Introduction to RELAX NG
  2. · Getting started with elements
  3. · Adding child elements
  4. · Adding attributes

print this article
SEARCH DEVARTICLES

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>


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials