Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

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

Include Page
XML Example
XML Example

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

Code Block
def xsd = '''
<xs:schema xmlns:xs="" elementFormDefault="qualified">
  <xs:element name="records">
        <xs:element maxOccurs="unbounded" ref="car"/>
  <xs:element name="car">
        <xs:element ref="country"/>
        <xs:element ref="record"/>
      <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:element name="country" type="xs:string"/>
  <xs:element name="record">
    <xs:complexType mixed="true">
      <xs:attribute name="type" use="required" type="xs:NCName"/>

import javax.xml.XMLConstants
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)))