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


    (Page 4 of 4 )

    So far, the assertions we've made have all dealt with residents. In fact, the XPath expressions we've used for the assertion tests have operated in the context of resident elements. This is of course consistent with the way Schematron works;  assertions are not just placed into a schema definition as-is. Assertions are grouped into rules. All of the assertions in a given rule share the same context. A rule context is a group of elements that the rule will act upon.

    It is possible, then, to group all the assertions we've made under a single rule acting upon the resident elements of a document. Let's go ahead and wrap all of the assertions we've made into a single rule element. The context attribute of the rule element contains the context expression which, in this case, would simply be the resident element. The context expression is an XPath expression.


    <rule context="resident">

    <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>

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

    Aliens cannot vote.

    </assert>

    <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>

    </rule>


    As you can see above, a rule by itself does not have to be complex. After all, it's just a group of assertions bundled together with the same context. Of course, the context expression could have been more complicated, but the basic principle is very simple.

    A more complex feature of rules is the ability to "inherit" from another rule. It's possible in Schematron to create a special rule called an abstract rule that by itself has no context. Other rules can then extend the abstract rule and provide a specific context. Notice how abstract rules are similar to abstract classes in programming languages. By themselves, they don't do anything, but they pass their members down to child classes.

    Abstract rules are used to save space by bundling common assertions together. For example, suppose that we created an element to represent a tourist, and that we put tourists along with residents in the same document. Both tourists and residents would share an age element. So, if we wanted to check for the presence of the element in both, we could create an abstract rule containing this assertion. The rule has to be given an identifier using the id attribute (so that we know what we're extending later), and we have to declare it abstract by using the abstract attribute. Let's go ahead and create this abstract rule:


    <rule id="personRule" abstract="true">

    <assert test="age">

    A resident has an age.

    </assert>

    </rule>


    In order to extend it in another rule, we'd simply introduce an extends element, whose rule attribute points to the abstract rule to be extended:


    <rule context="resident">

    <extends rule="personRule" />

    ...

    </rule>

    <rule context="tourist">

    <extends rule="personRule" />

    ...

    </rule>


    Above, we declare two rules, one which applies to residents and one which applies to tourists. Both rules will contain the assertion in the abstract rule without having to define it over again.

    Please check back next week for the second part of this 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.

     

    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