views:

495

answers:

5
+1  Q: 

XML Validation

Hi,

I want to validate an XML file against RELAXNG schema provided. Searching on the net gave me hints to create a Schema instance using SchemaFactory by providing the appropriate Schema Language. So as per my requirement I used:

*SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);*

But the following exception was thrown at run:

Exception in thread "main" java.lang.IllegalArgumentException: 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(SchemaFactory.java:207)

I am using Java6 and observed that only 'W3C_XML_SCHEMA_NS_URI' works. Rest all the schema URIs throws the similar exception.

I am fairly new to using the XML validation APIs. Can someone please provide me the appropriate usage in case I am not doing it correctly?

Thanks & Regards,
Keya

+1  A: 

RNG is poorly supported in Java APIs, it never really got traction. I know of no Java API implementations that handle it. I suggest converting the schema to XML Schema (using Trang or similar) and then validating against that.

skaffman
Hi,Thanks for the reply. I used as you suggested Trang to convert the rng to xsd. But I get error running it. Following is the command with the error: $ java -jar trang.jar -I rng -O xsd config_schema.rng config_schema.xsd xml-validation/trang-20081028/config_schema.rng:1:1: fatal: Content is not allowed in prolog.$Can you provide me some suggestion on the above error?
Make sure there's no whitespace or comments between the start of the the file and the start of the schema definition.
skaffman
Thanks. Apart from whitespaces I was also using the .rng extension instead of .rnc. Using the right extension converted the RNG Schema to XSD.
A: 

RNG is not supported as widely as XML Schema. You should always think of using XSD first.

xav0989
RNG is, however, an awful lot easier to write. It's a valid notion to write in RNG then convert to XSD later.
skaffman
I should try it out then! I have some huge XSD to write...
xav0989
Actually, make that RNC (RelaxNG Compact) is the easy one
skaffman
A: 

msv is in Java; I suppose it has a clear API you can use, but I've only ever used it on the command line and in scripts.

reinierpost
A: 

Kohsuke Kawaguchi apparently wrote a adapter library for jaxp to use a relaxng validator, see here and here

Valentin Rocher
A: 

I've had success in validating a document against a XML-based RelaxNG Schema using Java 6, check out :

http://stackoverflow.com/questions/1541253/how-to-validate-an-xml-document-using-a-relax-ng-schema-and-jaxp

Don't know if it's relevant, just pointing out :-).

Dr1Ku