Validating XML with a W3C XML Schema

This example assumes the following class is already on your CLASSPATH:

XmlExamples.groovy
class XmlExamples {
  static def CAR_RECORDS = '''
    <records>
      <car name='HSV Maloo' make='Holden' year='2006'>
        <country>Australia</country>
        <record type='speed'>Production Pickup Truck with speed of 271kph</record>
      </car>
      <car name='P50' make='Peel' year='1962'>
        <country>Isle of Man</country>
        <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
      </car>
      <car name='Royale' make='Bugatti' year='1931'>
        <country>France</country>
        <record type='price'>Most Valuable Car at $15 million</record>
      </car>
    </records>
  '''
}

We can validate that segment of XML against a Schema with the following code:

def xsd = '''
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="records">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="car"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="car">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="country"/>
        <xs:element ref="record"/>
      </xs:sequence>
      <xs:attribute name="make" use="required" type="xs:NCName"/>
      <xs:attribute name="name" use="required"/>
      <xs:attribute name="year" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="country" type="xs:string"/>
  <xs:element name="record">
    <xs:complexType mixed="true">
      <xs:attribute name="type" use="required" type="xs:NCName"/>
    </xs:complexType>
  </xs:element>
</xs:schema>
'''.trim()

import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory

def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(xsd)))
def validator = schema.newValidator()
validator.validate(new StreamSource(new StringReader(XmlExamples.CAR_RECORDS)))
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Feb 25, 2008

    Steve Jones says:

    When I attempt the above, I have troubles, which I've distilled to the following...

    When I attempt the above, I have troubles, which I've distilled to the following. In summary, for some reason, my groovy code can only see some of the constants (unfortunately, not the interesting ones) in XMLConstants.

    With Groovy

    code
    #!/usr/bin/env groovy
    
    import javax.xml.XMLConstants
    println("XMLNS_ATTRIBUTE: "       + XMLConstants.XMLNS_ATTRIBUTE)
    println("W3C_XML_SCHEMA_NS_URI: " + XMLConstants.W3C_XML_SCHEMA_NS_URI)
    
    results
    >WithGroovy.groovy
    XMLNS_ATTRIBUTE: xmlns
    Caught: groovy.lang.MissingPropertyException: No such property: W3C_XML_SCHEMA_NS_URI for class: javax.xml.XMLConstants
    	at WithGroovy.run(WithGroovy.groovy:5)
    	at WithGroovy.main(WithGroovy.groovy)
    

    With Java

    code
    import javax.xml.XMLConstants;
    
    public class WithJava {
    
        public static void main(String[] args) {
            System.out.println("XMLNS_ATTRIBUTE: "       + XMLConstants.XMLNS_ATTRIBUTE);
            System.out.println("W3C_XML_SCHEMA_NS_URI: " + XMLConstants.W3C_XML_SCHEMA_NS_URI);
        }
    
    }
    
    
    results
    >java WithJava
    XMLNS_ATTRIBUTE: xmlns
    W3C_XML_SCHEMA_NS_URI: http://www.w3.org/2001/XMLSchema