XML technologies / Schematron / Compact and modular programming / Variables

(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 7)

Variables are the main tool for compact code and are declared with element <let>.

A variable always has a name and a value. The value can be assigned when it is declared—in Schematron through XPath. Its scope is limited and depends on where it is defined. Within that scope it can be referenced by name.

Declaring variables

To declare a name and value, element <let> takes attributes name and value. As the attribute names suggest, name specifies the variable name as a string and value the value through an XPath expression:

<schema xmlns="http://purl.oclc.org/dsdl/schematron"> 
  <ns uri="http://www.schematron.info/arche" prefix="arc"/> 
  <let name="nutzlast" value="sum(//arc:gewicht)"/>         (1) 
  […] 
</schema> 

(1) Variable nutzlast receives the content of element <nutzlast>.

The <let> element may occur not only inside <schema> but also within <pattern>, <phase> and <rule>. Using variables within <rule> is especially useful: here the XPath expression may be relative to the rule’s context node. Each node selected as the rule’s context therefore determines a fresh, context-dependent variable value.

<rule context="arc:tier"> 
  <let name="art" value="arc:art"/> 
  <report  test="count(//arc:tier[arc:art=current()/arc:art]) &gt; 2">In der  Arche gibt es mehr als zwei Tiere der Art <value-of  select="arc:art"/>.</report> 
</rule> 

Depending on the context node, the variable takes the content of element arc:tier/arc:art. For a zebra this is string Zebra, for a lion string Löwe, and so on.

Referencing variables

In XPath expressions, prefix the variable name with $ to reference it: $variablenname.

<rule context="arc:tier"> 
  <let name="art" value="arc:art"/> 
  <report test="count(//arc:tier[arc:art=$art])  &gt; 2">In der Arche gibt es mehr als zwei Tiere der Art  <value-of select="$art"/>.</report>   (1) 
</rule> 

(1) Each subexpression previously accessing the context node’s <art> element can now be replaced with variable reference $art. This substitution leaves the test’s behavior unchanged.

The rule barely differs from code without variables, and even contains more characters. But it is better for extending or changing the schema. Suppose access to element <art> changes: one correction in the variable definition fixes it. Without a variable, every subexpression needs separate correction.

Variable scope

A variable is accessible only within the element declaring it. At the top level it is therefore valid throughout the schema; within a <rule> element it applies only to that rule. There are exceptions, at least with Skeleton: variables in <pattern> elements are treated as top-level variables. Rule-scoped variables can also be used in referenced <diagnostic> elements or, as we shall see, referenced abstract rules.

Background for XSLT experts: why these exceptions?

When transforming the schema, all <let> elements become corresponding <xsl:variable> elements. Thus,

<let name="art" value="arc:art"/>

becomes

<xsl:variable name="art" select="arc:art"/>

Transformation also changes the schema’s structure. There is no corresponding container in XSLT into which <pattern> could be transformed. Structure is instead organized through different template modes. Since variables cannot be organized that way, they must become top-level variables in the generated stylesheet. Rules become <xsl:template> elements and their associated <let> elements become <xsl:variable> elements.

The <diagnostic> elements and abstract rules can access variables because transformation integrates them into the corresponding rule’s templates. The rule’s context node thus also applies to the diagnostic.

   

 

 

 

back to top