XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Here’s how the Schematron program generates programs … program generating programs

Hi Folks,

Programs that generate programs are fantastic.

A Schematron processor is a program that generates programs. It takes as input a rules document (a .sch file) and it outputs a validator program (a .xsl file).

In this message I show how Schematron accomplishes this.

A “Schematron processor” is an XSLT program that outputs an XSLT program. Here are the Schematron processor’s inputs and outputs:

How can one XSLT program generate another XSLT program? The answer is with this remarkable XSLT element: the namespace-alias element. Here’s how the namespace-alias element is used:

                <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

That is read like this: "Hey XSLT processor, during output please swap the namespace prefix of any element that is prefixed by axsl with the xsl namespace prefix."

The content of the following XSLT template rule (in grey) is an alias XSLT program (i.e., each “XSLT element” is qualified by axsl):

During output the axsl namespace prefix will be replaced by an xsl prefix, resulting in an XSLT program!

The following graphic shows the XSLT program that is generated.

That, in a nutshell, is how Schematron generates programs.

Now let’s dig a bit deeper into the details of Schematron.

Suppose we want to validate the following BookStore document using Schematron.

<BookStore>
   
<Book>
       
<Title>Adventures of Huckleberry Finn</Title>
       
<Author>Mark Twain</Author>
   
</Book>
   
<Book>
       
<Title>The Elements</Title>
       
<Author>Euclid</Author>
   
</Book>
</BookStore>

And let’s suppose that we want this rule enforced:

                Book must contain a Title element and an Author element.

The Schematron rule element (sch:rule) specifies a context. Here is a Schematron rule that specifies Book as the context:

<sch:rule context="Book">
    ...
</sch:rule>

Contexts are set in XSLT using template rules. So a Schematron processor will output an XSLT program that has a template rule matching on Book:

The Schematron assert element (sch:assert) specifies a rule. Recall that in our example the rule is this:

                Book must contain a Title element and an Author element.

The following Schematron assert expresses the desired rule.

<sch:assert test="Title and Author">
    ...
</sch:assert>

That rule is simply an XPath expression. It can be expressed in XSLT using a choose statement:

<xsl:choose>
   
<xsl:when test="Title and Author"/>
   
<xsl:otherwise>
       
<xsl:sequence select="."/>
   
</xsl:otherwise>
</xsl:choose>

Read as: If there is a Title and an Author, then no action is taken (because the data is valid). Otherwise, the content of the assert is output.

Here’s a Schematron processor which generates an XSLT program containing a template rule matching on Book and within that template rule is a choose statement which evaluates the assert test condition:

The above output is a fully functional XSLT program which can be used to valid the BookStore XML document.

The “Schematron processor” is a program that outputs a program – neat!

Schematron has a lot more bells and whistles than I’ve shown above. Nonetheless, we have seen the core ideas underlying the Schematron processor.

I encourage you to use these ideas to write your own programs that generate programs!

/Roger

P.S. Here is the complete code for the (simplified) Schematron processor:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                         xmlns:sch="http://purl.oclc.org/dsdl/schematron"
                         xmlns:axsl="http://www.w3.org/1999/XSL/Transform/alias"
                         version="2.0">
   
<xsl:output method="xml"/>    
    
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
   
    
<xsl:template match="/">
       
<axsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                         version="2.0">
           
<axsl:output method="xml"/>
           
<xsl:for-each select="//sch:rule">
               
<axsl:template match="{@context}">
                   
<xsl:for-each select="sch:assert">
                       
<axsl:choose>
                           
<axsl:when test="{@test}"/>
                            
<axsl:otherwise>
                               
<axsl:sequence select="."/>
                           
</axsl:otherwise>
                       
</axsl:choose>
                   
</xsl:for-each>
               
</axsl:template>
           
</xsl:for-each>
       
</axsl:stylesheet>
   
</xsl:template>
</xsl:stylesheet>



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS