I just ant to jot down how to programmatically use Jing for Relax NG validation. It has a simple API but I could not find any description besides the JavaDoc. It took me some time to figure out how to use it.
If you want to validate a XML file, just use ValidationDriver. If you have a DOM Document or StAX source, just do the following
//we are using Relax NG compact format SchemaReader schemaReader = CompactSchemaReader.getInstance(); //schema can be reused, it's thread safe Schema schema = schemaReader.createSchema(ValidationDriver.fileInputSource(new File("your_schema.txt)), PropertyMap.EMPTY); //can use different error handler here (try DraconianErrorHandler http://www.thaiopensource.com/relaxng/api/jing/com/thaiopensource/xml/sax/DraconianErrorHandler.html) ErrorHandler seh = new ErrorHandlerImpl(); PropertyMapBuilder builder = new PropertyMapBuilder(); builder.put(ValidateProperty.ERROR_HANDLER, seh); //Validator is NOT thread safe Validator validator = schema.createValidator(builder.toPropertyMap()); Source source = ...//your XML source TransformerFactory.newInstance().newTransformer().transform(source, new SAXResult(validator.getContentHandler()));
And that’s it.
Here’s some XML:
1234567890
abcdefghijklmnopqrstuvwxyz
Here’s a Relax NG compact schema:
default namespace = “http://api.host.example”
namespace soap = “http://schemas.xmlsoap.org/soap/envelope/”
namespace ns = “http://host.example”
start =
element soap:Envelope {
element soap:Body {
element ns:someMethod {
element ns:in {
element foo { xsd:integer },
element bar { xsd:string { pattern=”moo” } }
}
}
}
}
With example code on this blog I get 4 errors:
3:6: error: found attribute “xmlns:soap”, but no attributes allowed here
6:14: error: found attribute “xmlns”, but no attributes allowed here
6:14: error: found attribute “xmlns:ns”, but no attributes allowed here
8:54: error: character content of element “bar” invalid; must be a string matching the regular expression “moo”
I can understand the last error but not the first 3. It looks like the namespaces are getting confused. Can you help?
Here’s some XML:
<?xml version=”1.0″ ?>
<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
<soap:Body>
<ns:someMethod xmlns=”http://api.host.example”
xmlns:ns=”http://host.example”>
<ns:in>
<foo>1234567890</foo>
<bar>abcdefghijklmnopqrstuvwxyz</bar>
</ns:in>
</ns:someMethod>
</soap:Body>
</soap:Envelope>
Here’s a Relax NG compact schema:
default namespace = “http://api.host.example”
namespace soap = “http://schemas.xmlsoap.org/soap/envelope/”
namespace ns = “http://host.example”
start =
element soap:Envelope {
element soap:Body {
element ns:someMethod {
element ns:in {
element foo { xsd:integer },
element bar { xsd:string { pattern=”moo” } }
}
}
}
}
With example code on this blog I get 4 errors:
3:6: error: found attribute “xmlns:soap”, but no attributes allowed here
6:14: error: found attribute “xmlns”, but no attributes allowed here
6:14: error: found attribute “xmlns:ns”, but no attributes allowed here
8:54: error: character content of element “bar” invalid; must be a string matching the regular expression “moo”
I can understand the last error but not the first 3. It looks like the namespaces are getting confused. Can you help?
Sorry, I am not RelaxNG expert so I am afraid I will not be able to help you.