Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Apply Schematron constraints to XForms documents automatically

How to use the free XML Forms Generator tooling to quickly apply formally defined constraints to a standards-compliant form

Jan Joseph Kratky (kratky@us.ibm.com), Senior Software Engineer, IBM 
Jan Joseph Kratky, the development lead for the XML Forms Generator and Visual XForms Designer, is a member of the W3C XForms Working Group. A Sun Certified Java Programmer and Sun Certified Web Component Developer, Mr. Kratky has worked with Java technologies since 1997 and with Eclipse technologies since 2001. He is currently a software engineer with IBM Emerging Software Standards in Research Triangle Park, N.C.
Kevin E. Kelly (kekelly@us.ibm.com), Senior Technical Staff Member, IBM 
Kevin E. Kelly is an IBM senior technical staff member who works on software standards. Mr. Kelly has been a member of the W3C XForms Working Group and is chairman of the W3C Compound Document Formats Working Group. His focus is on developing open standards-based technologies for faster, more efficient standards adoption through XML-based and model-driven approaches.
Steve Speicher (sspeiche@us.ibm.com), Senior Software Engineer, IBM 
Steve Speicher, the lead developer of the Compound XML Document Toolkit, is an IBM senior software engineer working on emerging standards. Mr. Speicher is a member of the W3C Compound Document Formats Working Group; he uses Model-Driven Development (MDD) to improve the development of standards. He has previously worked on "build" and SCM tools in the Rational division and in IBM internal tools.
Keith Wells (wellsk@us.ibm.com), Advisory Software Engineer, IBM 
Keith Wells is a software engineer at IBM in Research Triangle Park, N.C. Mr. Wells has been involved with Emerging Technologies and the Emerging Technologies Toolkit for several years. Currently, he is exploring opportunities with compound documents, model-driven development, software standards, and XML-based technologies.

Summary:  Hot on the heels of the release of the second edition of the XForms specification, IBM alphaWorks has released a new round of free tools, including the XML Forms Generator, to accelerate the development of forms that comply to this standard. The recent update lets you apply constraints defined in a Schematron 1.5 document to the generated form. Itself an XML markup, Schematron provides for the specification of business rules and data relationships that XML Schema cannot. While XForms natively provides for validation against XML Schema, any use of Schematron constraints must be built into the form itself. With development efforts already under way to integrate Schematron constraints with XForms -- most notably, the Instance Validator form built by Origo Standards, Ltd. (see Resources) -- automation of the application of these constraints is a natural next step.

Date:  27 Jun 2006
Level:  Intermediate
Also available in:   Chinese  Japanese

Activity:  10482 views
Comments:  

The World Wide Web Consortium (W3C) developed the XForms standard for the presentation and collection of form data. As stated in the W3C Recommendation, XForms is intended to be "the next generation of forms for the Web." As the Recommendation itself declares, "By splitting traditional XHTML forms into three parts -- XForms model, instance data, and user interface -- it separates presentation from content, allows reuse, gives strong typing -- reducing the number of round-trips to the server, as well as offering device independence and a reduced need for scripting."

XForms documents feature a data model that contains one or more XML instance documents. The form manipulates the instance documents and provides for the submission of that XML to a back-end system. Since Schematron is itself XML, XForms can easily treat it as part of the data model for a form.

XForms achieved a significant milestone with the release of the second edition of the XForms 1.0 specification on 14 Mar 2006. The update to the XML Forms Generator containing Schematron support became available on alphaWorks shortly thereafter.

What is the XML Forms Generator?

The XML Forms Generator provides a jump-start for form development. It quickly and automatically produces valid and functional forms containing XForms markup embedded within an XHTML document. The input to form generation can be an XML data instance with or without a backing XML Schema, or a Web Services Description Language (WSDL) document.

A plug-in to the open source Eclipse workbench (see Resources), the XML Forms Generator was first released on alphaWorks in Apr 2005. See Resources for a link to the alphaWorks page for the XML Forms Generator, where you can learn more and install the tooling.

What is Schematron?

XML Schema is widely used and well-suited for statically describing the structure and content of XML. It is, however, limited in terms of more-dynamic analysis of instances. For example, in XML Schema, you cannot constrain an XML document in this way: "The sum of the values of elements A and B must be equal to 100."

In Schematron, you can specify a constraint such as that easily. Like XML Schema, Schematron is itself XML, and is therefore a natural fit for XForms, which is itself an XML markup for manipulating XML data. With its small tag set and use of familiar syntax such as XPath, Schematron is easy to learn and write, yet powerful.

The International Organization for Standardization (ISO) is working toward the standardization of Schematron; a draft specification is available (see Resources). In the meantime, Schematron 1.5 has fairly broad acceptance, and the XML Forms Generator deals with Schematron 1.5 documents only.

A Schematron document is essentially a set of constraints that apply to XML data. You can express a concrete Schematron constraint in one of two ways:

  1. As an assertion, meaning that a test for a certain condition will be applied to an instance document. For an instance document to be valid, all assertions must evaluate to true.
  2. As a report, in which a test for a condition also will be applied. However, the meaning of the test is the converse of the meaning for assertions. That is, if the test evaluates to true, the document will be seen to be in an invalid state.

Schematron permits you to group assertions and reports together within constructs called rules, and then to group these rules together within patterns.

With XPath, you can specify test conditions, as well as specify a context in which the test condition would apply. A Schematron author can also provide text that is applicable when a condition is violated. This text is likely to be human-readable, targeted at some end user who is manipulating the data instance. Alternatively, the text can be more technical in nature, targeted at an XML developer constructing an XML instance that must conform to the constraints defined in a given Schematron document.

Throughout this article, we use the simple XML instance document in Listing 1.


Listing 1. A simple XML document
                
<?xml version="1.0"?>
<root xmlns="http://www.example.org"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.example.org Simple.xsd">
    <A>100</A>
    <B>0</B>
</root>

The XML instance document in Listing 1 references a schema called Simple.xsd, the contents of which are in Listing 2.


Listing 2. An XML Schema for the simple XML document
                
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.example.org" 
            xmlns:ex="http://www.example.org"
            elementFormDefault="qualified">           
    <xsd:element name="root" type="ex:rootType"/>                      
    <xsd:complexType name="rootType">  
        <xsd:sequence>    
            <xsd:element name="A" type="xsd:integer"/>
            <xsd:element name="B" type="xsd:integer"/>      
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

The XML Schema document in Listing 2 declares both element A and element B to be of type integer. However, XML Schema cannot define the constraint you want on the values of these two elements -- that their sum be equal to 100. However, you can easily define this constraint in Schematron, as shown in Listing 3.


Listing 3. A Schematron definition for the simple XML document
                
<?xml version="1.0"?>
<sch:schema xmlns:sch="http://www.ascc.net/xml/schematron">
    <sch:title>AB Example</sch:title>
    <sch:ns uri="http://www.example.org" prefix="ex" />
    <sch:pattern name="Our Only Pattern" id="pattern1">
        <sch:rule context="/ex:root" id="sum100">
            <sch:assert test="ex:A + ex:B = 100">
                The sum of the values of A and B must be 100.
            </sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema>

Even with the brief introduction to Schematron given so far, you can easily tell what this document does. For any XML data to which it applies, the sum of the values of A and B must be equal to 100, as is declared in the XPath expression in the test attribute of the lone assert element.

How do you specify a Schematron document in the XML Forms Generator?

In the XML Forms Generator, you use the global preference settings (Window > Preferences) under the XML Forms Generator preference category to specify a Schematron document you want to process during form generation.

You can take one of two actions on the specified Schematron document. We'll examine these in depth soon, but for now, it's enough to know that you can select one or both of these options to activate the Schematron schema location preference. You can then select Browse to specify the schema document you want to apply to your generated forms (see Figure 1).


Figure 1. The Schematron processing options in the XML Forms Generator
Schematron processing options in the XML Forms Generator

Sometimes, Schematron constraints are contained within an XML Schema document, usually within XML Schema appinfo elements. The XML Forms Generator, however, deals only with standalone Schematron documents.

How can you apply constraints to your form?

In XForms, you can specify a constraint to instance data that must be satisfied for that data to be submitted. The first option in the Schematron preference page, Apply constraints found in a schematron schema, signals the XML Forms Generator to apply the constraints specified in the Schematron document to any XForms document it generates.

XForms applies the constraint by binding it to the instance data. When you run the XML Forms Generator on the data in Listing 1 against the Schematron document of Listing 3 and select the Apply constraints option, it yields a form that has the XForms bind in Listing 4.


Listing 4. An XForms bind constraining the form submission
                
<xforms:bind nodeset="instance('instance_model_root')"  
             constraint="instance('instance_model_root')[ex:A + ex:B = 100]"/>

This XForms bind element has two attributes, nodeset and constraint, both of which take XPath expressions. In Listing 4, the nodeset attribute describes the nodes in the instance document that will be constrained. In the example, the XForms instance() function indicates that all data in the named instance is constrained. The constraint attribute contains a Boolean expression, which defines the condition to which the data is constrained. This XPath expression enforces the constraint you saw in the Schematron document -- that the sum of the values of elements A and B must be equal to 100.

If this expression doesn't evaluate to true, the data pointed to by the nodeset attribute (in this example, the entire document) will be considered invalid and cannot be submitted. Figure 2 shows the simple form generated by the XML Forms Generator, rendered in Mozilla Firefox.


Figure 2. The form in Firefox
The form in Firefox

In Figure 2, the constraint that was applied to the form becomes apparent if you change the values of A and B so that their sum equals something other than 100. Under these conditions, nothing will happen when you press the Submit button, since your binding is enforcing the fact that the data currently is not valid. On the other hand, if you provide acceptable values, pressing the Submit button will cause the XML data to be submitted.

How can you provide detailed messages on violated constraints?

XForms also provides the ability to hide or show controls dynamically, again by using a bind on the data, this time with a property called the relevant model item property (the XForms specification explains the XForms model item properties in detail -- see Resources). The other Schematron option provided in the XML Form Generator preferences (Figure 1), Generate validation message outputs from schematron messages, makes use of this mechanism to conditionally display the messages given in the Schematron document for individual asserts and reports.

In this case, you want to control the display of each message individually, based on the test specified by its associated Schematron assert or report. Therefore, each assert and report will have its own relevance bind, like the one in Listing 5.


Listing 5. Applying relevance to a Schematron message
                
<xforms:bind id="pattern_Our Only Pattern_rule_1_assert_1"
             nodeset="instance('instance_model_root_schematron')/
                      sch:pattern[@name='Our Only Pattern']/sch:rule[1]/sch:assert[1]"
             relevant="instance('instance_model_root')[not( ex:A + ex:B = 100 )]"/>

Listing 5 shows the bind that will be generated for the constraints given in the sample Schematron document. As was the case with the constraint binding, the bind element has a nodeset attribute that specifies the node within an XML data instance (in this case, the Schematron document) to which the binding will be applied. Each element also includes a relevant attribute, which specifies the condition under test for the corresponding assert or report in the Schematron document. The node specified by the nodeset attribute's value will be relevant only when the value of the relevant attribute evaluates to true. When a data node is relevant, a browser will display any XForms controls associated with that node. When a node is not relevant, any associated controls customarily will be hidden.

Since the Schematron document itself is made available to the form as instance data, you're free to bind controls to nodes in the Schematron document. You're interested in simply displaying text rather than providing any opportunity for data input for these nodes, so the xforms:output control is a natural choice.

Because data bindings already exist for applying relevance to the Schematron messages, you can use the bind attribute on the output elements to associate each with a message, as in Listing 6.


Listing 6. Providing for display of a Schematron message
                
<xforms:output bind="pattern_Our Only Pattern_rule_1_assert_1" 
               model="model_root"/>

The screen capture in Figure 3 shows the form as it appears in Firefox, with values whose sum does not equal 100.


Figure 3. The form in Firefox, with values that violate the Schematron constraint
The form in Firefox, with values that violate the Schematron constraint

Listing 7 gives the form's source in its entirety, after it has been generated with both the constraint-generation and message-generation options. Notice the two instances, as mentioned: one that references the XML instance data, and another that references the Schematron document.


Listing 7. The form's source
                
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xforms="http://www.w3.org/2002/xforms" 
    xmlns:sch="http://www.ascc.net/xml/schematron"
    xmlns:ex="http://www.example.org">
  <head>
    <title>AB Form</title>
    <xforms:model id="model_root" schema="../data/Simple.xsd">
      <xforms:instance id="instance_model_root" src="../data/Simple.xml"/>
      <xforms:instance id="instance_model_root_schematron" src="../data/Simple.sch"/>
      <xforms:bind id="pattern_Our Only Pattern_rule_1_assert_1" 
                   nodeset="instance('instance_model_root_schematron')/
                   sch:pattern[@name='Our Only Pattern']/sch:rule[1]/sch:assert[1]"
                   relevant="instance('instance_model_root')[not( ex:A + ex:B = 100 )]"/>
      <xforms:bind nodeset="instance('instance_model_root')" 
                   constraint="instance('instance_model_root')[ex:A + ex:B = 100]"/>
      <xforms:submission id="submit_model_root" ref="instance('instance_model_root')"
                         action="http://xformstest.org/cgi-bin/showinstance.sh" 
                         method="post"/>
    </xforms:model>
  </head>
  <body>
    <xforms:group>
      <xforms:label>AB Form</xforms:label>
    </xforms:group>
    <xforms:input ref="instance('instance_model_root')/ex:A" model="model_root">
      <xforms:label>A</xforms:label>
    </xforms:input>
    <xforms:input ref="instance('instance_model_root')/ex:B" model="model_root">
      <xforms:label>B</xforms:label>
    </xforms:input>
    <xforms:output bind="pattern_Our Only Pattern_rule_1_assert_1" model="model_root"/>
    <xforms:submit submission="submit_model_root">
      <xforms:label>Submit</xforms:label>
    </xforms:submit>
  </body>
</html>

Where do you go from here?

In this article, we've demonstrated the Schematron processing capabilities of the XML Forms Generator, using a simple Schematron document with a single assertion. The XML Forms Generator can process Schematron documents with any number of rule declarations, and it can handle Schematron report elements and Schematron assert elements equally well.

Of course, you might want to do other things with constraints in a form that are not quite as simple to automate. For example, when a constraint is violated, you might want to provide a link from the informative message to the field that is primarily responsible for the violation. For example, the Instance Validator form developed by Origo Standards Ltd. (see Resources) does provide mechanisms for navigating to the vicinity of an offending field. However, note that even in the simple example, you might not be able to easily determine a particular field that is at fault. For example, when the sum of A and B doesn't equal 100, you cannot say that the value for A alone or B alone is at fault.

The form you've generated doesn't reference a stylesheet, nor does it contain much in the way of formatting. The XML Forms Generator provides a rich set of options, not covered in this article, that you can use to enhance the appearance and functionality of a form. The XML Forms Generator comes with extensive documentation built into the Eclipse help system. Furthermore, it provides a wide variety of samples, including a simple Schematron example similar to the one examined here. In addition, you can use the Visual XForms Designer, also available on alphaWorks (see Resources), to touch up the generated form for your specific needs.

With these free tools at your command, you should be on your way to building fully functional, standards-compliant forms in no time.


Resources

Learn

Get products and technologies

  • XML Forms Generator: Create functional, standards-compliant forms with a click of the mouse using this Eclipse-based tool from alphaWorks.

  • Mozilla XForms: Render your standards-compliant forms in Mozilla Firefox using this plug-in.

  • Visual XForms Designer: Check out the home page, with links to installation instructions, prerequisites, and the forum.

  • Compound XML Document Toolkit: Explore other open-standard XML markups, including Scalable Vector Graphics (SVG), MathML, VoiceXML, and Synchronized Multimedia Integration Language (SMIL).

  • Eclipse: Get the open source Eclipse workbench and Web Tools platform.

Discuss

About the authors

Jan Joseph Kratky, the development lead for the XML Forms Generator and Visual XForms Designer, is a member of the W3C XForms Working Group. A Sun Certified Java Programmer and Sun Certified Web Component Developer, Mr. Kratky has worked with Java technologies since 1997 and with Eclipse technologies since 2001. He is currently a software engineer with IBM Emerging Software Standards in Research Triangle Park, N.C.

Kevin E. Kelly is an IBM senior technical staff member who works on software standards. Mr. Kelly has been a member of the W3C XForms Working Group and is chairman of the W3C Compound Document Formats Working Group. His focus is on developing open standards-based technologies for faster, more efficient standards adoption through XML-based and model-driven approaches.

Steve Speicher, the lead developer of the Compound XML Document Toolkit, is an IBM senior software engineer working on emerging standards. Mr. Speicher is a member of the W3C Compound Document Formats Working Group; he uses Model-Driven Development (MDD) to improve the development of standards. He has previously worked on "build" and SCM tools in the Rational division and in IBM internal tools.

Keith Wells is a software engineer at IBM in Research Triangle Park, N.C. Mr. Wells has been involved with Emerging Technologies and the Emerging Technologies Toolkit for several years. Currently, he is exploring opportunities with compound documents, model-driven development, software standards, and XML-based technologies.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Open source
ArticleID=129321
ArticleTitle=Apply Schematron constraints to XForms documents automatically
publish-date=06272006
author1-email=kratky@us.ibm.com
author1-email-cc=dwxed@us.ibm.com
author2-email=kekelly@us.ibm.com
author2-email-cc=dwxed@us.ibm.com
author3-email=sspeiche@us.ibm.com
author3-email-cc=clarkega@us.ibm.com
author4-email=wellsk@us.ibm.com
author4-email-cc=dwxed@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers