Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / XML

Getting Started with Schematron

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
16 Aug 2011CPOL4 min read 42.7K   19   2
The fastest way to get started with Schematron and Altova's XMLSpy®

Introduction

This article takes the first example of the excellent Schematron tutorial available here and shows how to create the XML input, the .sch Schematron schema file and how to instantly validate it all in Altova's XMLSpy® XML editor and the “XML ValidatorBuddy” plugin. Once you have seen how this works, you can also try the other examples in the tutorial in the same way.

Everything which is described here in the article is also shown at Schematron in XMLSpy - Chapter 2 as a Flash animation. Taking a look at the animation will help you to follow the content of this article.

Background

XML instance files are often validated against a W3C schema which usually is also an XML file using .xsd as file extension. There are a lot of XML parsers available which support this type of validation. A W3C schema describes the structure of the possible XML instances and the datatypes of elements and attributes. A W3C schema doesn’t provide any means to apply semantic checks to the XML instances. To complement this, Schematron provides a way to define rules to check XML at a semantic level.

How It Works

Before you can use Schematron validation in XMLSpy®, you need to install the “XML ValidatorBuddy” plugin. You can download the installer from Download.com.

The input XML and the Schematron schema are taken from this part of the tutorial. You can take the XML from there or just copy it from below and create a new document in XMLSpy®:

XML
<?xml version="1.0" encoding="utf-8"?>
<doc>
    <chapter id="c1">
        <title>chapter title</title>
        <para>Chapter content</para>
    </chapter>
    <chapter id="c2">
        <title>chapter 2 title</title>
        <para>Content</para>
    </chapter>
    <chapter id="c3">
        <title>Title</title>
        <para>Chapter 3 content</para>
    </chapter>
</doc> 

At this point, we need to save the XML input file. This is necessary for adding it afterwards to a Project folder in XMLSpy®. Use the “File | Save” command for this and save it anywhere you like. Let's say we choose input1.xml as the filename.

The XML ValidatorBuddy plugin will only validate the XML against the Schematron rules if both files are added to the same Project folder in XMLSpy®. For this reason we create a “Schematron” Project folder in the active Project. If no Project is loaded in XMLSpy® please create a new one or open an existing Project where you want to add the new folder. Right-click the new Project folder and open the Properties dialog using the last command on the bottom of the context menu. Type xml;sch as file extensions for this folder in the related edit field. Afterwards just close the dialog again.

To add input1.xml set input1.xml as the active document and select the new Project folder. Then use the “Project | Add Active File to Project” command to add it to the folder. The document appears immediately in the Project folder.

We want to create a new .sch file now but the "Create new document" dialog in XMLSpy® doesn't provide this file type by default. So we need to add this file extension. You can do this in the Options dialog on the “File types” tab. Use the “Add new file extension” button and type “sch” as extension. It is also helpful to type something like “Schematron Schema” in the Description field.

The next step is to create the Schematron schema. The tutorial provides an empty Schematron file template which we take to create a new .sch document in XMLSpy®:

XML
<?xml version="1.0" encoding="utf-8"?>
<iso:schema xmlns=http://purl.oclc.org/dsdl/schematron 
	xmlns:iso=http://purl.oclc.org/dsdl/schematron 
	xmlns:dp=http://www.dpawson.co.uk/ns# queryBinding='xslt2' 
	schemaVersion='ISO19757-3'>
    <iso:title>Test ISO schematron file. Introduction mode</iso:title>
    <iso:ns prefix='dp' uri='http://www.dpawson.co.uk/ns#'/>
    
    <!-- Your constraints go here -->

</iso:schema>

Use “Edit | Pretty-print XML text” afterwards to format the XML. This also checks if the XML created so far is well-formed.

Now we can go back to the tutorial to copy the Schematron rule which is part of the first chapter into the schema:

XML
<iso:pattern>
    <iso:rule context="chapter">
        <iso:assert test="title">A chapter should have a title</iso:assert>
    </iso:rule>
</iso:pattern> 

Copy the <iso:pattern> element from above and paste it into the previously generated Schematron schema where the <!-- Your constraints go here --> comment is. The complete schema should then look like this:

XML
<?xml version="1.0" encoding="utf-8"?>
<iso:schema xmlns="http://purl.oclc.org/dsdl/schematron" 
	xmlns:iso=http://purl.oclc.org/dsdl/schematron 
	xmlns:dp="http://www.dpawson.co.uk/ns#" 
	queryBinding="xslt2" schemaVersion="ISO19757-3">
    <iso:title>Test ISO schematron file. Introduction mode</iso:title>
    <iso:ns prefix="dp" uri="http://www.dpawson.co.uk/ns#"/>
    
    <!-- Your constraints go here -->
    <iso:pattern>
        <iso:rule context="chapter">
            <iso:assert test="title">A chapter should have a title</iso:assert>
        </iso:rule>
    </iso:pattern>
</iso:schema> 

This makes the Schematron schema complete and we can save it in order to add it to the Project folder in XMLSpy® where the input1.xml document is already added. I suggest a file name like chapter1.sch. Once the .sch Schematron schema is in the same Project folder as the XML input instance, we can validate it in XMLSpy® using the "Validate" button on the Schematron tab of the “XML ValidatorBuddy” plugin window. Without any modifications, input1.xml should be valid now. You can make the XML invalid for the Schematron schema by putting one of the title elements into a comment.

The XML is now invalid and the “XML ValidatorBuddy” plugin window reports the error.

Please note that it is not necessary to save the input instance to the disk before you can validate it. The “XML ValidatorBuddy” plugin takes the content directly from the XMLSpy® text view for validation.

History

  • 2011-August-16 Update of Web links
  • 2008-May-11 Article created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer xml-buddy.com
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThank you for this helpful demo? Pin
mdathena8-Sep-11 9:08
mdathena8-Sep-11 9:08 
GeneralRe: Thank you for this helpful demo? Pin
xmlbuddy8-Sep-11 10:55
xmlbuddy8-Sep-11 10:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.