Open oliviercailloux opened 3 years ago
I am also interested in this issue. My approach is basically the same as in your code.
I have found that factory.setSchema(schema);
doesn't seem to do anything. What does seem to apply the schema is this line:
reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
schemaFilename);
When I add this line the error message that I get after the parse has begun is:
s4s-elt-schema-ns: The namespace of element 'grammar' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.
If I comment out the line
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
then the error becomes
Document is invalid: no grammar found.
My guess is that the SAXParserFactory.newInstance();
method is returning an instance of a XML Schema parser, but what is needed is a RelaxNG-based parser. I have looked through the jing-trang source code but I cannot find an implementation of an implementation of SAXParseFactory which is what appears to be required to work with SAX, so maybe this plumbing was never developed? I'm just guessing here.
Here is a more complete example:
System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory");
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);
Schema schema = sf.newSchema(scf);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setSchema(schema);
try {
SAXParser parser = factory.newSAXParser ();
System.err.println("parser schema="+parser.getSchema());
XMLReader reader = parser.getXMLReader ();
reader.setFeature("http://xml.org/sax/features/validation", true);
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
schemaFilename);
reader.setFeature("http://apache.org/xml/features/xinclude", true);
reader.setFeature("http://xml.org/sax/features/namespaces", true);
reader.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
reader.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
reader.setErrorHandler (this);
reader.setContentHandler (this);
reader.setEntityResolver (this);
reader.parse (new InputSource(input));
As a workaround I am using trang to convert rng to xsd, and then validation with the built in XML Schema validator works just fine.
I would be nice to only work with RelaxNG, and not have to convert to xsd, but it's not a show stopper.
May I suggest to include in the README (or elsewhere) an example use for using Jing through the standard SAX API? (Issue #21 provides some partial example, but unfortunately the link provided there is dead.)
Here is an attempt of mine, so far unsuccessful.
Then test as follows. Using docbook howto.xml and docbook.rng.
The above test yields:
But that file does validate against that schema when not going through SAX (using
com.thaiopensource.validate.Schema schema = new AutoSchemaReader().createSchema(relaxSchema, countingErrorProperties); Validator validator = schema.createValidator(countingErrorProperties); contentHandler = validator.getContentHandler(); xmlReader = ResolverFactory.createResolver(PropertyMap.EMPTY).createXMLReader();
and so on…)Could you perhaps indicate what the recommended usage is for using Jing through the SAX interface?
Thank you for this useful library.