Hi,
Is there a way to validate a XSD schema?
I perform a validation of a XML file according to this schema with
<code>
SchemaFactory schemaFactory =
SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schemaXSD = schemaFactory.newSchema(fichierXSD);
Validator validateur = schemaXSD.newValidator();
validateur.validate(fichierXML);
</code>
but I would like as well to perform a validation of *the schema*.
Thanks.
-o--
Timbo - 28 Sep 2005 15:19 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> but I would like as well to perform a validation of *the schema*.
But against what are you validating it? The XML file is being
validated with respect to the format specified in the schema. I
think the best you could do with regards to the XSD schema is a
syntax check, and a few other checks the jaxb gives you, such as
no duplicate names etc. Anything other validation would need to be
by hand.
oziris - 28 Sep 2005 16:11 GMT
Hi Timbo,
It's what I thought at first but I read begins of pseudo-solution. I
find a kind of confirmation that it's possible or not.
-o--
oziris - 28 Sep 2005 16:15 GMT
Please read "I look for" instead of "I find". Sorry for my poor english
:-(
-o--
Chris - 28 Sep 2005 15:38 GMT
So you want a schema for a schema? You can just treat the schema as an
XML file and validate it as normal. If you are looking to make sure
it's a valid schema file there are standard xsd's and dtd's out there
that will validate schemas.
If that's not what you are looking to do, I guess you'll have to
elaborate on your question.
Thanks,
~Chris
oziris - 28 Sep 2005 15:46 GMT
Hi Chris,
Thanks for your reply.
Where could I get such standard xsd's ? I'm a little bit lost :-(
-o--
Chris - 28 Sep 2005 16:23 GMT
http://www.w3.org/2001/XMLSchema.xsd is the reference schema for the
schema language.
Beware it's big, and you really don't want to validate your schema
everytime you need to validate an XML file. But when you are authoring
your schema, it's useful, especially when you don't have a schema
authoring tool
~Chris
Ian Pilcher - 28 Sep 2005 17:37 GMT
> http://www.w3.org/2001/XMLSchema.xsd is the reference schema for the
> schema language.
Note that many parsers, including the one in Sun's JDKs can't handle
validating schemas. In general the best practical way to validate a
schema is to use it to validate an XML file.

Signature
========================================================================
Ian Pilcher i.pilcher@comcast.net
========================================================================
Chris - 28 Sep 2005 18:41 GMT
Why wouldn't any mature validating parser not be able to validate a
schema against the one I provided?
A schema (xsd) is just xml in a certain namespace just like any other
xml document. What am I missing?
Thanks,
~Chris
Ian Pilcher - 28 Sep 2005 18:48 GMT
> Why wouldn't any mature validating parser not be able to validate a
> schema against the one I provided?
Bugs?
> A schema (xsd) is just xml in a certain namespace just like any other
> xml document. What am I missing?
Try it.

Signature
========================================================================
Ian Pilcher i.pilcher@comcast.net
========================================================================
Chris - 28 Sep 2005 19:48 GMT
Once I stripped the DTD declaration (the <!DOCTYPE> section) at the
top, I had no problem validating schemas against the aformentioned
schema with org.apache.xerces.parsers.SAXParser. Sun uses xerces
AFAIK.
~Chris
Martin Honnen - 28 Sep 2005 15:39 GMT
> Is there a way to validate a XSD schema?
> I perform a validation of a XML file according to this schema with
[quoted text clipped - 4 lines]
> XMLConstants.W3C_XML_SCHEMA_NS_URI);
> Schema schemaXSD = schemaFactory.newSchema(fichierXSD);
I think that call compiles and validates the schema, you need to set up
an error handler to be informed about any errors during compilation and
validation of the schema e.g. do
schemaFactory.setErrorHandler(yourErrorHandler);
before you call newSchema. The error handler needs to implement
org.xml.sax.ErrorHandler.

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
oziris - 28 Sep 2005 15:55 GMT
Hi Martin,
I thought a SAXParseException was thrown even if no ErrorHandler were
defined.
That is not the case in my app.
-o--
Martin Honnen - 28 Sep 2005 17:05 GMT
> I thought a SAXParseException was thrown even if no ErrorHandler were
> defined.
I have just tried it here to set up a simple Error handler which reports
any errors/warnings to System.out and then I have tried it with a schema
that intentionally contains some errors (e.g. typos like minOcurs for
the minOccurs attribute) and those errors are reported to the error
handlers as follows:
Recoverable parse error: org.xml.sax.SAXParseException:
s4s-att-not-allowed: Attribute 'minOcurs' cannot appear in element
'element'.
where the error handler is set as follows:
schemaFactory.setErrorHandler(new ErrorHandler () {
public void error (SAXParseException parseException) {
System.out.println("Recoverable parse error: " + parseException);
}
public void fatalError (SAXParseException parseException) {
System.out.println("Fatal parse error: " + parseException);
}
public void warning (SAXParseException parseException) {
System.out.println("parse warning: " + parseException);
}
});
So that way you get any errors reported during compilation and
validation of the schema.

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Roedy Green - 28 Sep 2005 20:41 GMT
>I think that call compiles and validates the schema,
Does the schema itself get parsed, and validated every time you
validate an XML document? If so, this seems a hefty penalty to pay. I
would have expected the parsed schema to be cached somehow.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Martin Honnen - 29 Sep 2005 12:43 GMT
>>I think that call compiles and validates the schema,
>
> Does the schema itself get parsed, and validated every time you
> validate an XML document?
No, when you look in my answer you can see that "that call" refers to
Schema schemaXSD = schemaFactory.newSchema(fichierXSD);
so there the schema file is parsed and compiled into a
javax.xml.validation.Schema object instance.
That instance can then be used multiple times to create a validator.

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Roedy Green - 29 Sep 2005 15:09 GMT
>> SchemaFactory schemaFactory =
>> SchemaFactory.newInstance(
[quoted text clipped - 7 lines]
>before you call newSchema. The error handler needs to implement
>org.xml.sax.ErrorHandler.
I was enshrining a heavily commented variant of your code as an
example at http://mindprod.com/jgloss/xsd.html
I wondered why you had both Schemas and Validators. I would have
thought validate would be a method of Schema. When does the Schema
itself get parsed/validated. When you create the Schema or the
Validator?
Schema schemaXSD = schemaFactory.newSchema( new File ( "myschema.xsd"
) );
// Create a Validator capable of validating XML files according to my
custom schema.
Validator validator = schemaXSD.newValidator();

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy Green - 29 Sep 2005 16:09 GMT
>I wondered why you had both Schemas and Validators.
I think the answer is the Schemas have more functions in life than
acting as Validators. Therefore various functionality was split off
into separate classes.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Martin Honnen - 30 Sep 2005 12:46 GMT
> I was enshrining a heavily commented variant of your code as an
> example at http://mindprod.com/jgloss/xsd.html
That has
// get the custom xsd schema describing the required format for my XML
files.
Schema schemaXSD = schemaFactory.newSchema( new File ( "myschema.xsd" ) );
// hook up org.xml.sax.ErrorHandler implementation.
schemaFactory.setErrorHandler( myErrorHandler );
but you should set up the error handler first on the schema factory and
then call newSchema e.g.
// hook up org.xml.sax.ErrorHandler implementation.
schemaFactory.setErrorHandler( myErrorHandler );
// get the custom xsd schema describing the required format for my XML
files.
Schema schemaXSD = schemaFactory.newSchema( new File ( "myschema.xsd" ) );
if the aim is to have the error handler report any problems found while
parsing and compiling myschema.xsd.

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Roedy Green - 30 Sep 2005 23:03 GMT
>but you should set up the error handler first on the schema factory and
>then call newSchema e.g.
of course. Thanks for picking that up. Now corrected.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy Green - 28 Sep 2005 20:30 GMT
>Is there a way to validate a XSD schema?
see http://mindprod.com/jgloss/xsd.html
There are a number of external tools. I think way you do it inside
Java is to use the Xerces part of JAXP. I have not used the tool so I
can't be more specific. When you figure it out, please post a code
snippet I can include under that glossary entry.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
oziris - 30 Sep 2005 08:29 GMT
Hi all,
To end this thread I confirm the Martin solution which consists in
setting an ErrorHandler to the SchemaFactory.
That reports correctly all errors relating to the XSD schema grammar.
Merci beaucoup à tout le monde.
-o--
Roedy Green - 30 Sep 2005 09:23 GMT
>Merci beaucoup à tout le monde.
translation: many thanks to everyone.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
oziris - 30 Sep 2005 15:15 GMT
I guessed everybody knows the word "merci" ;-)