Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] XSD Schema and Validation problems

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Steve Loughran <steve.loughran AT gmail.com>
  • Cc: xom-interest <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] XSD Schema and Validation problems
  • Date: Mon, 19 Dec 2005 13:30:35 +0000

On 12/19/05, peter murray-rust <pm286 AT cam.ac.uk> wrote:
> I am trying to add XSD functionality to my CML-XOM system (JUMBO) and
> have run into several problems. I would appreciate any clues and hope
> that the description below is sufficient to give some clues. I am
> using XOM1.1+Eclipse3.1.1+junit+xerces+xml-api+xercesSamples on an XP
> system.
>
> (a) I have constructed simple XML and XSD files of the type:
> cml0.xsd:
> <xsd:schema
> targetNamespace="http://org.xml-cml.cml/schema";
> xmlns="http://org.xml-cml.cml/schema";
> xmlns:xsd="http://www.w3.org/2001/XMLSchema";
> elementFormDefault="qualified"
> >
> <xsd:element name="cml">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="molecule" minOccurs="0"
> maxOccurs="unbounded">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:any processContents="lax"
> minOccurs="0" maxOccurs="unbounded"/>
> </xsd:sequence>
> <xsd:attribute name="id"/>
> </xsd:complexType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
> </xsd:schema>
>
> cml0.xml
> <c:cml
> xmlns:c="http://org.xml-cml.cml/schema";
> xsi:schemaLocation="http://org.xml-cml.cml/schema cml0.xsd"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> >
> <c:molecule id="a1"/>
> </c:cml>

> [Note - I do not like the idea of adding schema references within the
> XML instance but it seems to be one of the current ways of using XSD.
> I do not actually like XSD at all, but feel that I am constrained to
> use it - it solves ca 10% of validation problems in chemistry.
> Schematron adds somewhat more but ultimately we need much more
> expressive semantics.]



>
> The example(s) validate under Xerces in the Eclipse environment (I
> use dom.Counter from the samples to exercise the validation:
> private void xercesValidate(String file) {
> System.out.println(" === xerces validation: "+file+"
> ====");
> String[] args = {"-v", "-s", "file://"+file};
> dom.Counter.main(args);
> }
>
> Under XOM I have at least two problems:
>
> (A) the *.xml files with schema references fail to parse under XOM
> even if no validation is called for. a typical message is:
> nu.xom.ParsingException: Illegal path character
> which appears to have a significant whitespace character if I write it as:
> [nu.xom.ParsingException: Illegal path character ] in
> [C:\pmr\schema23\examples\xsd\countType1.xml]
> None of my files have whitespace characters in their names but
> temporary filenames might have. I can remove this error simply by
> removing the xsi:schemaLocation attribute.

sometimes xerces doesnt like spaces in filenames. Those schema
locations should not have spaces in, but %20 bits where they go. You
can use File.toURI or something to create valid uris. But personally,
I'd leave them out altogether.

>
> (B) I have tried to switch on validation using the example in the FAQ:
> System.out.println(" === xom+Xerces validation:
> "+file+" ====");
> Document doc = null;
> try {
> XMLReader xerces =
> XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
>
> xerces.setFeature("http://apache.org/xml/features/validation/schema";,
> true);
> Builder parser = new Builder(xerces, /*true*/ false );
> doc = parser.build(new FileReader(file));
> } catch (Exception e) {
> fail("should not throw "+e);
> ...
> and I get errors of the following sort:
>
> [Warning] :5:2: schema_reference.4: Failed to read schema document
> 'cml0.xsd', 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>.
> [Error] :5:2: cvc-elt.1: Cannot find the declaration of element 'c:cml'.
> [Warning] :6:32: schema_reference.4: Failed to read schema document
> 'cml0.xsd', 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>.



you need to look at how you can point xerces at schema locations.
There are properties for that, and for generic JAXP parsers (as a
fallback)

http://cvs.sourceforge.net/viewcvs.py/smartfrog/core/components/xml/src/org/smartfrog/services/xml/utils/XmlConstants.java?view=markup

This is Ant 1.7's schemavalidate task:
https://svn.apache.org/viewcvs.cgi/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/SchemaValidate.java?view=markup

I use a catalog myself, which registers the stuff with xerces, but
also registers itself to resolve any URL, and returns the files that
are included on the classpath:

http://cvs.sourceforge.net/viewcvs.py/smartfrog/core/extras/cdl/src/org/smartfrog/sfcore/languages/cdl/CdlCatalog.java?view=markup
http://cvs.sourceforge.net/viewcvs.py/smartfrog/core/components/xml/src/org/smartfrog/services/xml/utils/XmlCatalogResolver.java?view=markup

I've found that matching on the last thing on the path works best, for
reasons I dont understand.



> I would be grateful for pointers as to how to mend this and also ways
> of schema validation which do not require embedding absolute or
> relative addresses within the XML instances.

use a catalog; hand it off to your parser to resolve things and return
files on the classpath; add **/*.xsd to your JAR. Add some logging to
the catalog for extra diagnostics of trouble.

-steve




Archive powered by MHonArc 2.6.24.

Top of Page