XML
  Home arrow XML arrow Datatypes and More in 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

Datatypes and More in RELAX NG
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-08-11

    Table of Contents:
  • Datatypes and More in RELAX NG
  • Using the grammar element
  • Creating named patterns
  • Creating named patterns, continued

  • 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


    Datatypes and More in RELAX NG


    (Page 1 of 4 )

    Welcome to the third part of a three-part series on RELAX NG. In this part, we will discuss datatypes, the grammar element, and creating named patterns. That's a lot of ground to cover, so let's get started.

    Datatypes in RELAX NG

    In the last two articles, if an element did not contain child elements, then it was either empty or it contained plain text. This works fine for some situations. For example, there's no other practical way to represent cities and states. However, in other situations, the text pattern just doesn't work. For example, the date attribute accepts any sort of text. We specified a date in the form YYYY-MM-DD, like this:

    <person date="2008-06-30">

    However, there's nothing to stop a person from putting the date in the form MM-DD-YYYY:

    <person date="06-30-2008">

    Nor is there anything to stop a user from doing something like this:

    <person date="a week or so ago">

    The document will still validate. Clearly, the text pattern is far too loose for a large number of situations, such as when representing a date. Thankfully, however, RELAX NG supports datatypes to remedy this situation. Instead of using the text pattern, it's possible to use stricter datatypes. RELAX NG itself actually only defines two datatypes, string and token. The string datatype takes strings as-is for comparison, without reducing whitespace, while the token datatype reduces whitespace.

    These two datatypes will only get you so far, though, and we still can't represent our date properly. However, in most implementations of RELAX NG, you can actually use XML Schema datatypes. This ability provides access to a number of datatypes beyond just string and token, and it will allow us, at the very least, to represent the date properly.

    The datatypes first need to be specified as a datatype library. This way, RELAX NG knows what datatypes it is working with. Using the XML syntax, this is done by setting the datatypeLibrary attribute. The value of this attribute takes the form of a URI. In the case of the XML Schema datatypes, this URI is “http://www.w3.org/2001/XMLSchema-datatypes.”

    Every element in the schema has this attribute, and if set, the element and any of its children will use the corresponding datatype library, unless its children specify something different. So, the value of the attribute is actually inherited. We can go ahead and set this attribute on the root element of the XML schema:

    <element name="people" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" xmlns="http://relaxng.org/ns/structure/1.0">

    Using the compact syntax, datatype libraries are identified using a prefix. The xsd prefix should already be set up for you, but if you wanted to set a prefix up yourself, you'd insert a line like this at the top of the schema definition:

    datatypes xsd = "http://www.w3.org/2001/XMLSchema-datatypes"

    Now the datatypes are ready to be used. To make use of datatypes using the XML syntax, the data element is used. This element will match the datatype specified by the type attribute. To represent the value of the person element's date attribute properly, we'd use the date datatype, which calls for a date in the form YYYY-MM-DD. Using the data element, the date attribute would be set up in the schema like this:

    <attribute name="date">
        <data type="date"/>
    </attribute>

    Using the compact syntax, the process is even simpler. In the compact syntax, the datatype is specified inside of an element's or attribute's brackets. So, only one word has to be changed in the compact schema:

    attribute date { xsd:date }

    Now something like this won't validate:

    <person date="a week or so ago">

    But a proper date will:

    <person date="2008-06-30">

    More XML Articles
    More By Peyton McCullough


       · Hello,This is the final part in my series on the RELAX NG schema language. The...
     

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek