Home Page › Forums › BizTalk 2004 – BizTalk 2010 › Handling Validation › Re: Handling Validation
There are a number of ways of validating an Xml message.
1. You can use schema validation, by defining the schema with the exact rules you require
e.g. using a regex pattern to constrain content
<xs:element name=”FirstName”>
<xs:simpleType>
<xs:restriction base=”xs:string”>
<xs:pattern value=”([a-zA-Z])*”/>
</xs:restriction>
</xs:simpleType>
</xs:element>
You can then use a number of ways of performing the validation. The XmlDisassembler and XmlValidtor pipeline components will both perform validation, however they will both throw an exception on the first error and you will need to implement Route Message on Error functionality.
You can implement your own validation component (C# class library) that will continue past the first error and produce a list of all errors discovered, This can be run as a pipeline component (you will need to use Route Message on Error functionality) or from an orchestration.
2. An alternative is to use a Schematron pattern http://www.schematron.com/
This is my preferred approach as it provides more scope for validation, especially with loosely defined schemas that used for a number of different functions i.e. some elements may be defined as minOccurs = 0 but they may be reuired for some functions but not others – for example an Id field that is not required on a Add but is required for an update.
I typically skip using the Schematron language and processors (they don’t produce the output I require) and code my own Xslt.
I perform the validation inside an orchestration using a Transform shape or a dynamic transform. The put message is like this
<validation report errorcount=”1″>
<validationerror><xpath>/Employees/Employee/FirstName</xpath><message>Can only contain alpha characters</message></validationerror>
</validation report>
After the map you can use a decide shape on errorcount and either send an error message or continue processing using the original message.