XML Schema (XSD) validation tool? [closed]
At the office we are currently writing an application that will generate XML files against a schema that we were given. We have the schema in an .XSD file.
Are there tool or libraries that we can use for automated testing to check that the generated XML matches the schema?
We would prefer free tools that are appropriate for commercial use although we won't be bundling the schema checker so it only needs to be usable by devs during development.
Our development language is C++ if that makes any difference, although I don't think it should as we could generate the xml file and then do validation by calling a separate program in the test.
Solution 1:
After some research, I think the best answer is Xerces, as it implements all of XSD, is cross-platform and widely used. I've created a small Java project on github to validate from the command line using the default JRE parser, which is normally Xerces. This can be used on Windows/Mac/Linux.
There is also a C++ version of Xerces available if you'd rather use that. The StdInParse utility can be used to call it from the command line. Also, a commenter below points to this more complete wrapper utility.
You could also use xmllint, which is part of libxml. You may well already have it installed. Example usage:
xmllint --noout --schema XSD_FILE XML_FILE
One problem is that libxml doesn't implement all of the specification, so you may run into issues :(
Alternatively, if you are on Windows, you can use msxml, but you will need some sort of wrapper to call it, such as the GUI one described in this DDJ article. However, it seems most people on Windows use an XML Editor, such as Notepad++ (as described in Nate's answer) or XML Notepad 2007 as suggested by SteveC (there are also several commercial editors which I won't mention here).
Finally, you'll find different programs will, unfortunately, give different results. This is largely due to the complexity of the XSD spec. You may want to test your schema with several tools.
UPDATE: I've expanded on this in a blog post.
Solution 2:
There's a plugin for Notepad++ called XML Tools that offers XML verification and validation against an XSD.
You can see how to use it here.
Solution 3:
xmlstarlet is a command-line tool which will do this and more:
$ xmlstarlet val --help XMLStarlet Toolkit: Validate XML document(s) Usage: xmlstarlet val <options> [ <xml-file-or-uri> ... ] where <options> -w or --well-formed - validate well-formedness only (default) -d or --dtd <dtd-file> - validate against DTD -s or --xsd <xsd-file> - validate against XSD schema -E or --embed - validate using embedded DTD -r or --relaxng <rng-file> - validate against Relax-NG schema -e or --err - print verbose error messages on stderr -b or --list-bad - list only files which do not validate -g or --list-good - list only files which validate -q or --quiet - do not list files (return result code only) NOTE: XML Schemas are not fully supported yet due to its incomplete support in libxml2 (see http://xmlsoft.org) XMLStarlet is a command line toolkit to query/edit/check/transform XML documents (for more information see http://xmlstar.sourceforge.net/)
Usage in your case would be along the lines of:
xmlstarlet val --xsd your_schema.xsd your_file.xml
Solution 4:
For Windows there is the free XML Notepad 2007. You can select XSD's for it to validate against
UPDATE: better yet, use Notepad++ with the XML Tools plugin