Complete XML Schema Validation
I am looking for a tool that will tell me all of the XML Schema validation failures. All the other tools I have looked at so just tell me the first couple, and then I have to fix those before it will tell me the next errors. I realize that some errors may be dependent on other nodes being in different orders, but things like data types being formatted wrong should be able to be reported even if the nodes are in the wrong order.
I have alrea开发者_运维问答dy looked through the other tools suggest for other questions on here, and they all stop after the first failure. So if one of those tools will do what I want, then please let me know the steps to accomplish that.
A programming library or technique that will let me do this in .NET or Delphi would work to.
I use XMLNotepad a lot. (I don't know if you've already tried it.)
Its'free and does a good job at reporting a lot of errors simultaneously, provided they are independent and not terminal! About the same as the compiler errors in Delphi: once it has started on a false track everything following is often nonsense.
Anyway, it has worked very well for me to validate against quite intricated schemas like the TVB 3.2...
The issue at hand is that an XML Schema defines the language for your XML document. The validator then just tries to 'compile' your XML document with that language specification.
For most compilers having fixed languages (like Delphi or C#), it took quite a while to become relaxed enough in order to report more than one error.
XML Schemas are more like a dynamic language, so I estimate that would take vendors of validation tools even longer to make them relaxed enough to report more than one error, or continue after the first error.
I'm not sure of any products doing this, but there are some patents filed for things that might do it: http://www.faqs.org/patents/app/20080228810 and http://www.faqs.org/patents/app/20080229292
Note these have been filed in 2008!
--jeroen
This code in C# should do just that - it will scan the entire XML and validate it against the XSD provided, and it will spit out all validation errors (or errors in the schema, too!) as they happen. Hope this helps!
The way you use it would be:
MyXmlValidator (name of XML file) (name of XSD file)
That's all there is!
Marc
static void Main(string[] args)
{
ShowTitle();
if(args.Length < 2)
{
ShowUsage();
return;
}
string xmlFileName = args[0];
string xsdFileName = args[1];
if(!File.Exists(xmlFileName))
{
ShowError(string.Format("XML File '{0}' does not exist)", xmlFileName));
return;
}
if (!File.Exists(xsdFileName))
{
ShowError(string.Format("XSD schema '{0}' does not exist)", xsdFileName));
return;
}
StreamReader xsdReader = new StreamReader(xsdFileName);
XmlSchema Schema = new XmlSchema();
Schema = XmlSchema.Read(xsdReader, new ValidationEventHandler(XSDValidationEventHandler));
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
ReaderSettings.ValidationType = ValidationType.Schema;
ReaderSettings.Schemas.Add(Schema);
ReaderSettings.ValidationEventHandler += new ValidationEventHandler(XMLValidationEventHandler);
XmlTextReader xmlReader = new XmlTextReader(xmlFileName);
XmlReader objXmlReader = XmlReader.Create(xmlReader, ReaderSettings);
while (objXmlReader.Read())
{ }
Console.WriteLine("Successful validation completed!");
}
private static void XSDValidationEventHandler(object sender, ValidationEventArgs e)
{
Console.WriteLine("Error reading XSD: " + e.Message);
}
private static void XMLValidationEventHandler(object sender, ValidationEventArgs e)
{
Console.WriteLine("Validation error: " + e.Message);
}
private static void ShowError(string message)
{
Console.WriteLine("ERROR: " + message);
}
private static void ShowUsage()
{
Console.WriteLine("USAGE: GaraioXmlValidator (name of XML file) (name of XSD file)" + Environment.NewLine);
}
private static void ShowTitle()
{
Console.WriteLine("GaraioXmlValidator v1.0 (c) 2008 by Garaio Technology Lab" + Environment.NewLine);
}
I think the problem is that once a schema is invalid for any one reason, anything else that follows is unreliable so any further validation failures could be misleading. The need to perform validation beyond "is this document valid" is perhaps too specialised a need to have attracted the interest of tools companies.
From the point of view of a validator for the vast majority of uses it will be put to, it doesn't make sense to continue validation once a failure is encountered. The document cannot become any more invalid than it already is and cannot be discovered to be valid once it has been determined to be invalid.
Perhaps it would be useful to understand why you wish to do this?
e.g. if you are trying to format a document to conform to a schema there are tools which should generate an example document instance from a given schema for you, to act as reference or example/template.
I discovered that OxygenXML editor does a pretty good job of this as well. It is a commercial editor, but the validation is done with the Xerces engine to do the validation (with an option to use others.) I tried Xerces before, and couldn't get it to report past the first error, but they say they force it to keep reporting. It does a really nice job.
XMLSpy officially does not support this. Based on what I saw of the two editors, I like OxygenXML much better then XMLSpy, not just for that feature either (which is really nice the way it does it).
精彩评论