[XML] XSD Schema Validation

O

oziris

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

Timbo

oziris said:
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.
 
C

Chris

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
 
M

Martin Honnen

oziris wrote:

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.
 
O

oziris

Hi Chris,

Thanks for your reply.
Where could I get such standard xsd's ? I'm a little bit lost :-(

-o--
 
O

oziris

Hi Martin,

I thought a SAXParseException was thrown even if no ErrorHandler were
defined.
That is not the case in my app.

-o--
 
O

oziris

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

Chris

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
 
M

Martin Honnen

oziris wrote:

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.
 
C

Chris

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
 
I

Ian Pilcher

Chris said:
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.
 
C

Chris

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
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
M

Martin Honnen

Roedy said:
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.
 
R

Roedy Green

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.

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

Roedy Green

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.
 
O

oziris

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top