Advertisements

oziris wrote:
> 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*.
>
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.

Advertisements

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.

> 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);

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.

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

> 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:

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.

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.

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.
--
Canadian Mind Products, Roedy Green.http://mindprod.com Again taking new Java programming contracts.

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.

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?

On Thu, 29 Sep 2005 14:09:54 GMT, Roedy Green
<> wrote or quoted :
>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.
--
Canadian Mind Products, Roedy Green.http://mindprod.com Again taking new Java programming contracts.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!