Thursday, January 29, 2009

XML Schema Regular Expressions

The regular expressions used in the XML Schema are of a different flavour other than the ones used regularly in Java. Not all pattern matching techniques apply directly in XML Schema.

For example to validate the decimal places I use the following pattern "\-{0,1}[0-9]*(\.{1}[0-9]{1,2})?" without the codes, what this pattern says is that the negative symbol is optional and if provided only one character of the negative symbol should be given. The asterisk denotes that any number of numerals may be provided, followed by a opening bracket and a pattern and closing bracket followed by a "question" mark symbol. The question mark symbol means that the pattern in the brackets is optional. In effect this pattern can accept whole numbers and optional decimal numbers.

Once you have defined that the node type is decimal and applied that above pattern. Then you can write a simple java class that reads the XML document and also applies the XSD to validate that XML document. And if the XML document contains invalid characters for that node it throws an excpetion saying that the value is not supported by that pattern.

No comments:

Post a Comment