This article demonstrates how to validate an XML document against an RelaxNG Schema by using Woodstox. See
for further details on RelaxNG XML schema specification.
Create employee.rng as shown below.
It defines the employee element that contains id, name and salary sub-elements.
Create validEmployee.xml as shown below. It is in accordance with employee.rng described above.
Create invalidEmployee.xml as shown below.
Note that it is
in accordance with employee.dtd described above as it defines the ‘title’ element (see line 4 below) instead of ‘salary’ element.
Create TestRelaxNgValidation as shown below.
method to validate the XML document against the XML schema (see line 29 below).
From the main method, first successfully validate the validEmployee.xml file (see line 45 below).
Then try to validate invalidEmployee.xml (see line 46 below)and verify that the validation fails .
The output of the program demonstrating both validation success and failure scenarios is shown below.