I am trying to validate a xml file against relax ng.Since jdk does not have relax validator,I am adding jing.jar library but still the SchemaFactory factory
= SchemaFactory.newInstance("XMLConstants.RELAXNG_NS_URI"); throws illegalArgument Exception: "No SchemaFactory that implements the schema language specified by: http://relaxng.org/ns/structure/1.0 could be loaded
at javax.xml.validation.SchemaFactory.newInstance(Unknown Source)
In the article "Java Validation API" it is mentioned that adding relax ng library will solve the above said issue.
Please guide me how to achieve validation of xml against relax ng.Also tell me how to create a relax ng (.rnc) file in eclipse.I can only create a notepad file.Will that be accepted by eclipse if the 1st issue i resolved?
Please help.Thanks in advance.
SystemAdmin 110000D4XK1830 Posts
Re: xml validation2010-12-01T15:42:03ZThis is the accepted answer. This is the accepted answer. Just for the record, the article being referred to is by Elliotte Rusty Harold
no doubt a very useful and excellent article.
To achieve validation of xml against relax ng, it is indeed a bit tricky if you don't get the line of reasoning right and some technical detail.
 Add the jing.jar to the classpath: that's what you no doubt have done.
[1.1] No other jar is needed to add to the classpath, actually, for the purpose. (When one does not get the program work, one tends to try adding this or that in additional. That's not needed.)
 This is the critical step.
[2.1] In the article, there already provides the hint, namely,
How schema factories are located The Java programming language isn 't limited to a single schema factory. When you pass a URI identifying a particular schema language to SchemaFactory.newInstance(), it searches the following locations in this order to find a matching factory: 1. The class named by the "javax.xml.validation.SchemaFactory:schemaURL" system property 2. etc... 3. etc... 4. etc...
[2.2] The trouble till now is that the correct SchemaFactory has failed to be located. And the remedy is to make it known in the system property that would make sure it would find it before looking for some other location.
 This is how to make the correct SchemaFactory being located.
[3.1] Add this line to set the system property before instantiating a factory
//if your rng is written in XML Syntax, use this. System.setProperty( "javax.xml.validation.SchemaFactory:"+XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory"); /* //if your rng is written in Compact Syntax, use this instead. System.setProperty("javax.xml.validation.SchemaFactory:"+XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); */ //continue the same as in the article SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); //etc etc...
[3.2] Where to find their locations? use the jar viewer to discover the path to the SchemaFactory. From the viewer, you follow the path in the namespace above. (And the jaxp is also being alluded to in the article.)
That is all you need to do with the demo in the article to validate using relax ng schema.