Saturday, December 17, 2011

Schematron validation


Schematron is rule-based validation language.  It is different from XML schema validation and it is supplement to XML schema validation.  If required you can apply both of them to the validation of the XML document.

Schematron defines a set of assertions that are to be enforced. If all these assertions are met, the document is seen as valid.

The assertion is specified using XPath.

Schematron has 4 key elements: pattern, assert, rule and ns.

The assert element has an attribute of test whose value is defined as XPath expression. This expression should return a Boolean value. If the test expression evaluates to true, then assertion has been met.

The assert is defined within a rule element.

Multiple asserts may be defined in a rule element. Each rule has a context attribute, which also contains an XPath expression used to specify the nodes to which the asserts will be tested.

The context may return zero, one or more nodes from XML document instance. Each node will be tested against all asserts within this rule.


Here is one example:  We need to validate that the MSISDN should start with 04 and totally has 10 digits and CAC should be numeric.

What we do here is to define one patter in which there are two contexts defined.   The context is using XPath to get the right element in XML document.  One context is for MSISDN element and another for CAC element.  In each element there is one assertion.  The assertion contains the test statement which uses the Regular expression to validate the element value.

<sch:schema xmlns:sch="http://www.ascc.net/xml/schematron">
                <!-- define the process namespace for finding the elements in the rule-->
                <sch:ns uri="http://toic.com/orderManagement/messages/v1" prefix="tns"/>
                <sch:ns uri="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20" prefix="xp20"/>
 
                <sch:pattern name="Check the parameters in ExecuteOrdre request">
                                <!-- context is the super variable that can be queried for validation-->
      
                              <!-- Check MSISDN as Identification has validate value -->   
                                <sch:rule context="tns:ExecuteOrderRequest[ xp20:ends-with(@*[local-name()='type'], 'CustomerOrder') ]/tns:Party/tns:PartyRole/tns:Identification[xp20:ends-with(./*[local-name()='Type'], 'MSISDN')] ">
                                                <sch:assert test="xp20:matches( string(tns:Value), '\b04\d{8}\b')">MSISDN should be 10 digit numeric starting with 04</sch:assert>
                                </sch:rule>

                              <!-- Check CAC as Identification has validate value -->   
                                <sch:rule context="tns:ExecuteOrderRequest[ xp20:ends-with(@*[local-name()='type'], 'CustomerOrder') ]/tns:Party/tns:PartyRole/tns:Identification[xp20:ends-with(./*[local-name()='Type'], 'CAC')] ">
                                                <sch:assert test="xp20:matches( string(tns:Value), '\b\d{1,}$')">CAC should be numeric</sch:assert>
                                </sch:rule>

                </sch:pattern>
</sch:schema>



 

No comments:

Post a Comment