This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Why isn't the parser validating an xml against a schema Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Why isn Watch "Why isn New topic
Author

Why isn't the parser validating an xml against a schema

michael shea
Greenhorn

Joined: Nov 11, 2001
Posts: 9
Guys, as the subject suggests, i have turned on validation via:

The reason I've set dynamic to true is because if an xml file does not include a schema to validate against, then I do NOT want to validate the xml file, however, if a schema has been specified, then i want to validate the xml file. The problem is that the xml files are never validated regardless of whether or not an xml file has specified a schema. (I've also sabotaged a few xml files to deliberately introduce known errors into the xml so i can see if it has been validated).
Thanks for any information.
Edit:
I forgot to say which parser: Its Xerces 1.4 using the DOM Parser.
[This message has been edited by michael shea (edited November 11, 2001).]
arul senthil
Greenhorn

Joined: Jul 03, 2001
Posts: 29
Hi
It seems only Xerces-J2 can validate an Xml against a Schema.
May be bcos Schema is relatively a new Standard. Pls visit the site http://xml.apache.org/xerces2-j/index.html for Xerces-Java 2.0.0 beta3 download & samples.
Regards,
Senthil
michael shea
Greenhorn

Joined: Nov 11, 2001
Posts: 9
Thanks for the reply. I actually may have the said the wrong version because we can validate against a schema. It works fine without the setFeature(dynamic, true) line in the code, except it validates all files, including those without schema's specified, which results in a whole bunch of errors being displayed)
michael shea
Greenhorn

Joined: Nov 11, 2001
Posts: 9
Actually the more i think about it, i realise that this may be a bug!?!? in the software. Could this be the case?
arul senthil
Greenhorn

Joined: Jul 03, 2001
Posts: 29
May be a bug.
If "http://xml.org/sax/features/validation" is set, then the Xml document MUST specify a grammar. i.e. a Schema file is necessary.But if "http://apache.org/xml/features/validation/dynamic" is set, then the Xml document is validated IF the grammar is specified.i.e. it depends on the Schema referral from Xml file.
Doesn't these two options appear complementary?
arul senthil
Greenhorn

Joined: Jul 03, 2001
Posts: 29
I couldnt try any samples so far. Just see this page if it helps : http://xml.apache.org/xerces-j/schema.html
Regards,
Senthil
michael shea
Greenhorn

Joined: Nov 11, 2001
Posts: 9
Originally posted by arul senthil:

Doesn't these two options appear complementary?

Yes, good point but I've also tried that and it still doesn't work. I've also tried using the sample DOMCount.java that came in the Xerces package. It too has the same problem. Could it be because the schema is incorrectly declared in the xml file? Here is a sample:

arul senthil
Greenhorn

Joined: Jul 03, 2001
Posts: 29
Xerces-j 1.4.x itself supports Schemas - sorry i didnt know that. A sample program i tried with Xerces-j 2.0.0 beta3 also gave the same problem.
The page http://www.apache.org/~edwingo/jaxp-faq.html says that :
Q: When I turn on validation, why do I fail to get any errors?
A: This is probably because you have not set an ErrorHandler. To get validation errors, three things must be true:
(1)The source document must be associated with a schema. For example, the source document contains a DOCTYPE declaration.
(2)Validation must be turned on.
(3)The application must set an ErrorHandler.
Often times, applications fail to perform the last item.
Any further improvements possible?
Regards,
Senthil
michael shea
Greenhorn

Joined: Nov 11, 2001
Posts: 9
Yep, I've set the errorHandler and it still has the same problem. I beginning to think that the Dynamic feature is not yet implemented. I may have to email the Xerces-J-User mailing list to see if anyone knows more about this.
[This message has been edited by michael shea (edited November 13, 2001).]
Rob Bass
Ranch Hand

Joined: Aug 28, 2001
Posts: 67
I am managing an XML Repository so validation is key for us. I tried Dynamic validation and it did not work either, I emailed IBM and they said it does not work in the 1.4.3:
Rob,
We had a bug in latest XML4j release, and that is why dynamic validation
was not working properly. We fixed the problem in Xerces (xml.apache.org)
however you will have to wait till next reference release of XML4J to get
this fix.

IBM Canada

I have not tried it since I found out that every XML file in the repository must have a grammar.
It is fixed, but where to find it I do not know...
michael shea
Greenhorn

Joined: Nov 11, 2001
Posts: 9
Guys, just to let you know and anyone else who has run into the same problem. I updated the Xerces parser to the latest (1.4.4) and the dynamic feature works correctly as it should.
Thanks for all the help.
arul senthil
Greenhorn

Joined: Jul 03, 2001
Posts: 29
Hi Micheal
Can u share the sample code (or just a skeletal outline) with me?Pls send to agsenthil@lycos.com OR just paste here.
Thanks & Regards,
Senthil
michael shea
Greenhorn

Joined: Nov 11, 2001
Posts: 9
Senthil, I'm not sure what code you wanted, but here is the code that I use and it dynamically validates xml files with the 1.4.4 parser.
arul senthil
Greenhorn

Joined: Jul 03, 2001
Posts: 29
Hi Michael
Thats what I wanted. Many thanks for that.
Regards,
Senthil
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why isn't the parser validating an xml against a schema