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

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
  1. · An Introduction to RELAX NG
  2. · Getting started with elements
  3. · Adding child elements
  4. · Adding attributes

print this article

An Introduction to RELAX NG - Adding attributes
(Page 4 of 4 )

We've defined two elements within the schema, but what about attributes? RELAX NG actually seeks to treat elements and attributes as similarly as possible, so throwing some attributes into the mix isn't that difficult.

Say that we want to add a date attribute to the person element. It could represent the date that the entry was added into the document. The instance document might look something like this:

    <person date="2008-06-20"/>
    <person date="2008-06-21"/>

In order to declare an attribute in the XML schema, the attribute element is used. It's placed as a child of the definition of whatever element the attribute belongs to, just as a child element would be. Also just as with a child element, the attribute element has a name attribute which corresponds to the name of the attribute being added. If we add the attribute declaration to the XML schema, the result would look like this:

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

Notice how we're able to remove the empty element without any complaints from RELAX NG. RELAX NG expects the element element to have at least one child element, but it doesn't care if it's an attribute or something such as the empty element.

You're probably able to guess what the compact schema would look like. Since, as was mentioned earlier, RELAX NG seeks to treat elements and attributes in the same way, adding an attribute is just like adding a child element. The resulting schema would look like this:

element people {
    element person {
        attribute date { text }

Above, the attribute is defined within the person element. We've declared that the attribute will have text within it. However, we did not explicitly do this in the XML schema. Using the XML syntax, the attribute element is, unlike the element element, allowed to be empty. The default datatype (we'll discuss datatypes in more detail later on) will be set to text, although it's perfectly possible to specify this manually:

<attribute name="date">

Above, we modify the attribute declaration, adding a text element, although it's certainly not required. Note, however, that a content type is required using the compact syntax.

That's all we have time and room for today. Check back next week for the second part of this three-part 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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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