Versions Compared

Key

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

...

Include Page
GROOVY:XML Example
GROOVY:XML Example

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:'http://iso-relax.sourceforge.net/', jar:'isorelax.jar')
// require(groupId:'org.iso_relax.verifier.jaxp.validation', artifactId:'isorelax-jaxp-bridge', version:'1.0')
// require(url:'https://msv.dev.java.net/', jar:'msv.jar')

def rng = '''
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <ref name="records"/>
    </start>
    <define name="car">
        <element name="car">
            <attribute name="make">
                <data type="token"/>
            </attribute>
            <attribute name="name">
                <text/>
            </attribute>
            <attribute name="year">
                <data type="integer"/>
            </attribute>
            <ref name="country"/>
            <ref name="record"/>
        </element>
    </define>
    <define name="country">
        <element name="country">
            <text/>
        </element>
    </define>
    <define name="record">
        <element name="record">
            <attribute name="type">
                <data type="token"/>
            </attribute>
            <text/>
        </element>
    </define>
    <define name="records">
        <element name="records">
            <oneOrMore>
                <ref name="car"/>
            </oneOrMore>
        </element>
    </define>
</grammar>
'''.trim()

import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
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: