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

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 - Getting started with elements


    (Page 2 of 4 )

    Let's get started with RELAX NG. The most fundamental thing to do with any grammar-based schema language is to represent an element. So, we'll start by examining a very simple XML document.

    Consider an XML document representing a group of people. This document might have a people element as the root element, which has child elements named person to store data about individual people. Only, for now, let's trim the document down so that only the root element is present:

    <?xml version="1.0" encoding="UTF-8"?>
    <people>
    </people>

    Creating a schema built around this document isn't very difficult at all because only a single element is present. The only thing we have to keep in mind is content—what does the people element contain? Normally, it would contain other elements, but since nothing is in it at the moment, we'll treat it as an empty element. So, although this parts from the example a bit, the following XML would be equivalent (but just for now!):

    <people/>

    We'll start with a schema using RELAX NG's XML syntax. Elements are represented using the element element. An element can be given a name with the name attribute. In a schema, the element tag can actually serve as the root element. We don't have to wrap it in another special element, although the validator will do so behind the scenes if we don't. But I'm getting ahead of myself here, and we'll get to that part later.

    Since we decided to treat the people element as an empty element, we must mark it so using the empty element, which must be a child of element. We must also add the appropriate namespace declaration, using “http://relaxng.org/ns/structure/1.0” as the URI. Here is the complete schema for the above XML, using RELAX NG's XML syntax:

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

    That wasn't too bad at all. Looking at the schema, it's very easy to tell exactly what an instance document would look like.

    Now let's take a look at the schema using the compact syntax. The compact syntax uses some of the same vocabulary as the XML syntax. Elements are declared using element, and the element is declared to be empty using empty. However, the result is remarkably concise. Here's what it looks like:

    element people { empty }

    Above, the element's name immediately follows element, and the element's children are contained in curly brackets. The schema built using the compact syntax is shorter than the first schema, yet the two produce the exact same result.

    If we want the element to contain text rather than be empty, then the transformation isn't very difficult. We simply need to substitute empty with text. Here's what the XML schema would look like:

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

    And here's what the compact schema would look like:

    element people { text }

    More XML Articles
    More By Peyton McCullough


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