views:

8155

answers:

11

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.

Thanks!

A: 

I found this blog post by googling: http://marknelson.us/2002/01/01/xml-schema-validation/

It's a bit dated, but looks like an alternative with code samples to using libxml.

Jason Dagit
+1  A: 

W3.org provides an online validator. I've used that in the past with great results, but only interactively, but you may be able to automate tests against it with a URL library.

Joe Skora
+2  A: 

I use Xerces:

http://xerces.apache.org/xerces-c/

John at CashCommons
+2  A: 

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:

xmlstartlet val --xsd your_schema.xsd your_file.xml
Charles Duffy
Do you know how serious the warning is about not fully supporting xml schemas?
Jason Dagit
Judging by other answers xmlstarlet is a bit weak on its xsd implementation.
Jason Dagit
+18  A: 

After some research, I think the best answer is Xerces, as it implements all of XSD, is cross-platform and widely used. The stdinparse utility can be used to call Xerces from the command line.

I assume the Java and C versions of Xerces agree on validation.

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. If someone has a simple script that can be called from a DOS prompt, perhaps they'd like to post it.

You could also use xmllint, which is part of libxml. You may well already have it installed. It can be used like:

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 :(

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.

Adrian Mouat
+1 for the xmllint program.
Cristian Ciupitu
I found the following helpful:http://jmvanel.free.fr/xsd/README.html
Matthew Hegarty
+1 for xmllint.
Trevor Hartman
+1  A: 

I don't believe the online validator response is correct. That validator seems to validate only a schema, it doesn't check that a particular document is well formed wrt a schema.

+1  A: 

http://www.xmlvalidation.com/

(Be sure to check the " Validate against external XML schema" Box)

-1, doesnt work in firefox
jcollum
A: 

The online XML Schema Validator from DecisionSoft allows you to check an XML file against a given schema.

+4  A: 

There's a plugin for Notepad++ that offers XML verification & validation against an XSD.

Nate
This plugin claims that all my files are valid, even when they aren't. (npp 5.7, XMLTools 2.3.1 unicode beta2 r805)
Ben
-1, this plugin is a pain to install
jcollum
A: 

A tool for quick and easy XML validation is available at http://www.xml-tools.com

You just need to run the installer and after that you can validate XML files directly from Windows File Explorer or the command-line.

xml-tools.com
A: 

For Windows there is the free XML Notepad 2007. You can select XSD's for it to validate against

SteveC