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

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

    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">
        <zeroOrMore>
            <element name="person">
                <attribute name="date"/>
            </element>
        </zeroOrMore>
    </element>

    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">
        <text/>
    </attribute>

    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.

       · 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 3 hosted by Hostway
    Stay green...Green IT