You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Zoltan Bryant <zo...@hotmail.com> on 2002/07/17 09:37:52 UTC

Validating a schema is a valid schema

Does anyone know how to validate whether a schema is a valid schema
Without creating an instance document and then validating that first?

I have code which uses a SAX parser and works when validating an xml
instance document against its schema however Im trying to use this
same code to pass in a schema and the Schema reference and validate one
against the other. e.g.

validate(myXMLDoc, "/home/mySchema.xsd") -- works
valiadte(mySchema, "/home/XMLSchema.xsd") -- fails

Does anyone know if this approach is valid or even if Xerces 2.0.1
Supports validating a schema directly in this way?

The code Im using is listed below. Im getting the following error
when I run it...

"schema_reference.4: Failed to read schema document 'null', because
1) could not find the document; 2) the document could not be read; 3) the 
root element of the document is not <xsd:schema."

...even though the document does exist on the file system, can be
read by my application and does have the root element <xsd:schema> for both 
schemas.

Any help anyone can give would be much appreciated.
Cheers

-----------

private static String validationFeat
=		"http://xml.org/sax/features/validation";

private static String schemaValidationFeat
=	"http://apache.org/xml/features/validation/schema";

private static String extSchemaProp
=		"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";

private boolean validate(InputSource xmlSource, String schemaURL) {

valid = true;
try {
	SAXParser parser = new SAXParser();

	// configures the xerces parser for schema validation.
	parser.setFeature(validationFeat, true);
	parser.setFeature(schemaValidationFeat, true);

        //validate against the given schemaURL
	parser.setProperty(extSchemaProp, schemaURL);

	// binds custom error handler to parser
	ErrorHandler handler = new ValidatingHandler();
	parser.setErrorHandler (handler);

	// parse (validates) the xml
	parser.parse(xmlSource);

} catch (SAXNotRecognizedException snre) {
         System.out.println("SAX not recognised " + snre);
	valid = false;
} catch (SAXException se) {
                System.out.println("SAX parser " + se);
		valid = false;
} catch (Exception e) {
          System.out.println("error parsing " + e);
          valid = false;
}

   return valid;
}






_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: Validating a schema is a valid schema

Posted by Eddie Robertsson <er...@allette.com.au>.
Hi Zoltan,

> validate(myXMLDoc, "/home/mySchema.xsd") -- works
> valiadte(mySchema, "/home/XMLSchema.xsd") -- fails
>
> Does anyone know if this approach is valid or even if Xerces 2.0.1
> Supports validating a schema directly in this way? 

Yes, this is possible to do but unfortunately Xerces 2.0.1 has a few 
bugs with regards to using the SchemaForSchemas to validate a schema 
document. There are two problems:

1) Xerces doesn't allow the use of "xs:anySimpleType" in schemas which 
will cause Xerces to fail.
2) The SchemaForSchemas place facet restrictions on the anySimpleType 
which also fails.

So, this means that you will get errors in the SchemaForSchemas before 
Xerces even tries to validate your schema document.

> private static String extSchemaProp
> =        
> "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation"; 


This is your problem. you can't use the 
external-noNamespaceSchemaLocation property because the schemas you're 
trying to validate use a targetNamespace so you have to use the 
following property:

"http://apache.org/xml/properties/schema/external-schemaLocation"

and to validate a W3C XML Schema it should be set to the following value:

"http://www.w3.org/2001/XMLSchema schemaURL"

Cheers,
/Eddie

> private boolean validate(InputSource xmlSource, String schemaURL) {
>
> valid = true;
> try {
>     SAXParser parser = new SAXParser();
>
>     // configures the xerces parser for schema validation.
>     parser.setFeature(validationFeat, true);
>     parser.setFeature(schemaValidationFeat, true);
>
>        //validate against the given schemaURL
>     parser.setProperty(extSchemaProp, schemaURL);
>
>     // binds custom error handler to parser
>     ErrorHandler handler = new ValidatingHandler();
>     parser.setErrorHandler (handler);
>
>     // parse (validates) the xml
>     parser.parse(xmlSource);
>
> } catch (SAXNotRecognizedException snre) {
>         System.out.println("SAX not recognised " + snre);
>     valid = false;
> } catch (SAXException se) {
>                System.out.println("SAX parser " + se);
>         valid = false;
> } catch (Exception e) {
>          System.out.println("error parsing " + e);
>          valid = false;
> }
>
>   return valid;
> }
>
>
>
>
>
>
> _________________________________________________________________
> MSN Photos is the easiest way to share and print your photos: 
> http://photos.msn.com/support/worldwide.aspx
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org