Versions Compared

Key

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

...

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: