Examples

First let's start by initializing the Schematic. All known schema files must be registered prior validating or using autocompletion. The xsd:schemaLocation attribute is ignored.

SchemaPool pool = new SchemaPool(new ValidationContextImpl());
URL schemaURL = new URL("file:///someschema.rng");
pool.getDefaultFactory().registerSchema("http://sample.namespace", new SchemaFactoryImpl.SchemaMetadataBean(schemaURL, false));

Now the schema is registered but not yet loaded. The pool serves as a cache of schema instances; it uses soft reference to cache schema instances. If we want to make sure that the schema instance stays in memory, we should use the SchemaReferences class which uses strong references instead. However we will use DocumentSchema helper object, which itself contains the SchemaReferences instance and offers some helper methods:

DocumentSchema docSchema = new DocumentSchema(document, pool);
docSchema.loadSchemas();

The loadSchemas() method will attempt to instantiate schema instance for each namespace present in the document. Now you may use the autocompletion support:

  • To query if an attribute is deletable, use
    docSchema.isDeletableAttribute(attr);
  • To get the datatype name of the value of the attribute:
    docSchema.getAttributeRule(attr).getDatatypeName();
  • To get list of attributes insertable into an element:
    docSchema.getElementRule(element).getInsertableAttributes();
  • To validate the document:
    docSchema.validate();