Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Include Page

We can validate that segment of XML using Relax NG with the following codeA number of RELAX NG validators are available (e.g. MSV and Jing). Rather than using the APIs for these validators directly, you might want to consider using the ISO RELAX project's common JARV API for accessing validators. Using this common API, you can switch between the available validators without changing your source code.

An even better option (if you are using Java 5 or above, or otherwise have JAXP 1.3 available to you) is to use the ISORELAX JARV to JAXP 1.3 Xml Validation Engine Adaptor. This hooks into the built-in JAXP validation Factory support in JAXP and makes the code that you use to access the RELAX NG validator the same as you would do for a W3C XML Schema validator.

Here is what the code would look like:

Code Block
// require(url:'', jar:'isorelax.jar')
// require(groupId:'org.iso_relax.verifier.jaxp.validation', artifactId:'isorelax-jaxp-bridge', version:'1.0')
// require(url:'', jar:'msv.jar')

def rng = '''
<grammar xmlns=""
        <ref name="records"/>
    <define name="car">
        <element name="car">
            <attribute name="make">
                <data type="token"/>
            <attribute name="name">
            <attribute name="year">
                <data type="integer"/>
            <ref name="country"/>
            <ref name="record"/>
    <define name="country">
        <element name="country">
    <define name="record">
        <element name="record">
            <attribute name="type">
                <data type="token"/>
    <define name="records">
        <element name="records">
                <ref name="car"/>

import javax.xml.XMLConstants
import javax.xml.validation.SchemaFactory

def factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(rng)))
def validator = schema.newValidator()
validator.validate(new StreamSource(new StringReader(XmlExamples.CAR_RECORDS)))

See also: