Fri
9
Dec '05
Usefull Errormessages With Schema and XInclude
by Frank Spychalski filed under articles, Work

If you find this useful, you might like my other articles, too.

As I promised yesterday, here’s the workaround for the problem that Xerces only provides a not very errormessages in case of an error in an included document when using Schema and Xinclude. If there is a error in the main document you get the normal error messages, explaining why your document is not valid (e.g. if we change an id attribute to ID we get “Attribute 'ID' is not allowed to appear in element 'e'“) but if the same error is made in the included element, the errormessage says: “Error on line 12 of document file:///home/psycho/workspace/Tests/xinclude/test.xml: Error attempting to parse XML file (href='include.xml').“.

There is a simple workaround, which sadly wastes a lot of cpu cycles, but I can’t think of another simple solution with less overhead.

  1. parse document with schema validation and XInclude, if there is no exception (which should be the default case) return result
  2. if there is an exception parse document again with XInclude but without schema validation
  3. if there is another exception, throw this exception (something is wrong with XInclude)
  4. write the result to String and parse the String yet again, this time with schema validation but without XInclude (we included everything in the second step)
  5. this will fail again, but the exception will include a useful errormessage

Any comments? Or questions? Just leave a Reply: