XML Validation problem  
Author Message
brian.monteith





PostPosted: 2004-7-30 1:07:00 Top

java-programmer, XML Validation problem I am having alot of trouble getting a XML document validated with a
schema.

I got a sample document and schema off of w3schools.com, which passed
an online xml validator:
http://tools.decisionsoft.com/schemaValidate.html. I cannot, however,
get them validated programmatically.

The documents are:

node.xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified"><xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element></xs:schema>

node.xml:

<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

I'm using java 1.5 currently, although I had problems with 1.4 and
xerces, so I'm pretty confident the problem isn't related to the java
version.

My code is:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
SchemaFactory sFact =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

factory.setSchema(sFact.newSchema(new java.io.File(SCHEMA_FILE)));

DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
public void warning (SAXParseException exception) throws SAXException
{
System.err.println(exception);
}
public void error (SAXParseException exception) throws SAXException {
System.err.println(exception);
}
public void fatalError (SAXParseException exception) throws
SAXException {
System.err.println(exception);
}
});

org.w3c.dom.Document doc = builder.parse(file);


The error I recieve is:

org.xml.sax.SAXParseException:
http://www.w3.org/TR/xml-schema-1#cvc-elt.1?note
org.xml.sax.SAXParseException:
http://www.w3.org/TR/xml-schema-1#cvc-elt.1?note
Exception in thread "main" java.lang.IllegalStateException
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$3.checkState(ValidatorHandlerImpl.java:411)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$3.getElementTypeInfo(ValidatorHandlerImpl.java:441)
at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.elementAug(JAXPValidatorComponent.java:299)
at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.endElement(JAXPValidatorComponent.java:291)
at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:163)
at com.sun.org.apache.xerces.internal.jaxp.validation.XNI2SAXEx.endElement(XNI2SAXEx.java:108)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:818)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:339)
at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:163)
at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent.endElement(JAXPValidatorComponent.java:206)
at com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler.endElement(XIncludeHandler.java:790)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1241)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1685)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:248)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:169)
...

I am behind a web proxy, and I don't know if the validation process
involves retrieving files at the URLs in the xml and xsd files, which
could be the problem.

can someone shed some light on this?

Thanks in advance.
 
vicient





PostPosted: 2004-8-30 20:27:00 Top

java-programmer >> XML Validation problem I think it is the bug of java 1.5. If you get validator from Schema, it
works.

 
vicient





PostPosted: 2004-8-30 20:28:00 Top

java-programmer >> XML Validation problem I think it is the bug of java 1.5. If you get validator from Schema, it
works.

 
 
steve_marjoribanks@hotmail.com





PostPosted: 2006-3-1 20:17:00 Top

java-programmer >> XML Validation problem I've posted this here and in the comp.lang.java.programmer group
because I'm not really sure if it's a Java problem or an XML problem!

Basically I have written a Java application which loads an XML file and
validates it (it is going to do other stuff with it as well once I get
this validation problem sorted!). I am using JDOM and Xerces-J 2.7.1.
It starts to validate fine but then reports a whole load of errors
which I obtained the details of using Java. The first few look like
this:

WARNING: schema_reference.4: Failed to read schema document
'http://schemas.opengis.net/gml/3.1.1/base/feature.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>. at line 16, column
121 in entity
http://www.dur.ac.uk/s.j.marjoribanks/geotechml/rawData.xsd

WARNING: schema_reference.4: Failed to read schema document
'http://schemas.opengis.net/gml/3.1.1/base/feature.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>. at line 10, column
121 in entity http://www.dur.ac.uk/s.j.marjoribanks/agsml/agsml.xsd

WARNING: schema_reference.4: Failed to read schema document
'http://schemas.opengis.net/gml/3.1.1/base/feature.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>. at line 9, column 121
in entity
http://www.dur.ac.uk/s.j.marjoribanks/agsml/holeInformation.xsd

WARNING: schema_reference.4: Failed to read schema document
'AgsmlBase.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>. at line 10, column 43 in entity
http://www.dur.ac.uk/s.j.marjoribanks/agsml/holeInformation.xsd

WARNING: schema_reference.4: Failed to read schema document
'http://schemas.opengis.net/gml/3.1.1/base/feature.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>. at line 9, column 121
in entity
http://www.dur.ac.uk/s.j.marjoribanks/agsml/laboratoryTesting.xsd


The first few errors are all like that, reporting that it could not
access a schema file and then there are loads which i think are
probably as a result of the schema file being inaccessible.
My schema imports quite a few other schemas, but there only seems to be
a problem loading 3 of them. The AgsmlBase.xsd one which is in the same
directory as the target schema of my instance document, the
http://www.schemas.opengis.net/gml/3.1.1/base/feature.xsd schema and an
XLinks one as well (not shown in the errors above). I don't understand
why it is not able to access them though? If I validate my file in
XMLSpy2006 it is ok (apart from one nasty bug which the Altova support
team inform me is a problem with the software). I have checked the URLs
and the schemas are definitley there and I can access them so why can't
it access them?

One though I did have was that if one of them is already imported into
one of the schemas which I import, could this cause a problem? It
doesn't in XMLSpy though and I don't think it should really?

Thanks

Steve

 
 
steve_marjoribanks@hotmail.com





PostPosted: 2006-3-2 1:48:00 Top

java-programmer >> XML Validation problem Ok, well I've fixed the problem with the agsmlBase schema, one of the
schemas had a typo and was trying to find AgsmlBase instead.

However, the problem still remains with the other one
(http://schemas.opengis.net/gml/3.1.1/base/feature.xsd). It says it
either can't find it, can't access it or it's not a schema. However, if
you navigate to the URI in a web broswer it is obviously available and
a valid schema as well. I tried validating it using the XSV validator
(http://www.w3.org/2001/03/webdata/xsv) and it said it was fine.

Why when I try and validate my file using Xerces (and Crimson) in my
Java application can it not find the schema mentioned above even though
it is obviously there? I validated the schema I am using with the XSV
tool also and it said that too was fine.

Anyone got any idea?!!!

Steve

Steve