XML
  Home arrow XML arrow Page 3 - Using Schematron
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

Using Schematron
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-01-07

    Table of Contents:
  • Using Schematron
  • Schematron, a rule-based schema language
  • Making assertions
  • Rules

  • 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


    Using Schematron - Making assertions


    (Page 3 of 4 )

    We'll start at the "bottom" of Schematron and then make our way up until we have arrived at a complete schema definition. The simplest unit in Schematron is an assertion. An assertion in Schematron is actually written out in plain language. Earlier, we made an assertion dealing with a resident's age and eligibility to vote: "If the resident's age is below 18, then the resident should not be able to vote." Taken as-is, this, of course, will do nothing. While an assertion is written out in plain language, it must also contain an XPath expression to back it up. This expression makes up the assertion test. The assertion test for our voter eligibility assertion would look like this:

    (age < 18 and eligible_to_vote = 'no') or age > 18


    Above, either the voter is under eighteen and not able to vote, or the voter is over eighteen. Any under condition will result in the XPath expression evaluating to false, which will cause the assertion to be unmet and the document not to validate.

    So, how would this look in a schema definition? Let's get to the actual markup used to create this assertion:


    <assert test="(age &lt; 18 and eligible_to_vote = 'no') or age > 18">

    If the resident's age is below 18, then the resident should not be able to vote.

    </assert>


    Above, you can make out the assertion, which makes up the inner content of the assert tag, and the assertion test, which is contained in the test attribute.

    The assertion we made demonstrates how a value contained in one element can affect the validity of a value contained in another element. This assertion demonstrates one of Schematron's greatest strengths: it can define validity in complex ways. We can, of course, keep adding assertions for a more complex result. For example, we could bring in other factors that affect a person's eligibility to vote, such as whether or not a resident is even a citizen. This would create yet another relationship for which to test.

    Let's go ahead and create the relevant assertion for this. A status tag could be added with two possible values: "alien" and "citizen." The assertion should read, "Aliens cannot vote." The complete markup would look like this:


    <assert test="(status = 'alien' and eligible_to_vote = 'no') or status = 'citizen'">

    Aliens cannot vote.

    </assert>


    Assertions don't have to be complex, though. They can also be very simple. For example, we could create an assertion to check for the mere presence of an element. Let's check to make sure that each resident element has an age element, a status element, and an eligible_to_vote element:


    <assert test="age">

    A resident has an age.

    </assert>

    <assert test="status">

    A resident has a status.

    </assert>

    <assert test="eligible_to_vote">

    A resident must have a voter eligibility value.

    </assert>


    More XML Articles
    More By Peyton McCullough


     

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