XMLForm's How-To Step 2
http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Main

How-Tos
Index

XMLForm Wizard
Introduction
Step 1
Step 2
Step 3
Step 4
Step 5

Step 2: Validation

The next step, validation, is optional. You do not have to validate your forms. Because we believe it's very easy to do so, we will do so here.

In the sitemap there are two parameters, xmlform-validator-schema and xmlform-validator-schema-ns. If you leave them empty, then no validation will be carried out. xmlform-validator-schema contains the name of the xml schema file we are using. xmlform-validator-schema-ns is the validator namespace we are using. Here is how the two parameters are referenced in the sitemap.

      
<map:parameter name="xmlform-validator-schema-ns" 
  value="http://www.ascc.net/xml/schematron"/>
  
<map:parameter name="xmlform-validator-schema" 
  value="schematron/wizard-xmlform-sch-report.xml"/>
          
      

Copy the "howto-xmlform-sch-report.xml," and place it in the folder src\scratchpad\webapp\samples\xmlform\howto\schematron.

The schema shown below is simple. It validates only the registration.xml page. The phase element includes information concerning the xml page to be validated. The id attribute contains the page name. The phase element contains an active element whose pattern attribute matches up to the pattern element below. Each pattern can contain one or more rule elements. Each rule element has a context attribute which maps to a javaBean value, for example userName. Nested inside the rule element is one or more assert element. Each assert element contains a test attribute. The value of test can be used to check certain criteria, for example, if a passed value is more than seven characters long. Nested between the beginning and closing assert tags is an error message text to be displayed when the test fails.

        
<?xml version="1.0" ?>

<schema ns="http://xml.apache.cocoon/xmlform"  
        xmlns="http://www.ascc.net/xml/schematron">

  <title>Schema for the XML Form example</title>
  
  <phase id="registration">
    <p>For user identity information.</p>
    <active pattern="reg"/>
    </phase>
    
  <phase id="confirm">
    <p>For final total validation and tracking 
       some tricky problems.</p>
    <active pattern="reg" />
  </phase>
    
  <pattern name="User Info Validation Pattern" id="reg">
    <rule context="/userName">
      <assert  test="string-length(.) &gt; 7">
	   Username should be at least 8 characters.
      </assert>
	  <assert  test="string-length(.) &lt; 20">
	    Username should be less than 20 characters.
      </assert>
	</rule>
	<rule context="/password">
	  <assert  test="string-length(.) &gt; 7">
	      Password should be at least 8 characters.
      </assert>
	  <assert  test="string-length(.) &lt; 20">
	    Password should be less than 20 characters.
     </assert>
	</rule>
	<rule context="/email">
	  <assert test="contains( string(.),'@')">
	    Email format is invalid.
      </assert>
	</rule>		
  </pattern>
</schema>

      

Now you are ready for Step 3: Form instance model

Copyright © 1999-2002 The Apache Software Foundation. All Rights Reserved.