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.
This question is related to
xml
validation
xsd
schema
I tend to use xsd from Microsoft to help generate the xsd from a .NET file. I also parse out sections of the xml using xmlstarlet. The final free tool that would be of use to you is altovaxml, which is available at this URL: http://www.altova.com/download_components.html .
This allows me to scan all the xml files picking up which xsd to use by parsing the xml.
# Function:
# verifyschemas - Will validate all xml files in a configuration directory against the schemas in the passed in directory
# Parameters:
# The directory where the schema *.xsd files are located. Must be using dos pathing like: VerifySchemas "c:\\XMLSchemas\\"
# Requirements:
# Must be in the directory where the configuration files are located
#
verifyschemas()
{
for FILENAME in $(find . -name '*.xml' -print0 | xargs -0)
do
local SchemaFile=$1$(getconfignamefromxml $FILENAME).xsd
altovaxml /validate $FILENAME /schema $SchemaFile > ~/temp.txt 2> /dev/null
if [ $? -ne 0 ]; then
printf "Failed to verify: "
cat ~/temp.txt | tail -1 | tr -d '\r'
printf " - $FILENAME with $SchemaFile\n"
fi
done
}
To generate the xml I use: xsd DOTNET.dll /type:CFGCLASS & rename schema0.xsd CFGCLASS.xsd
To get the xsd name I use: xmlstarlet sel -t -m /XXX/* -v local-name() $1 | sed 's/ $//'
This allows me to pickup the correct XSD using an element tag within the xml file.
The net result is that I can call a bash function to scan all the XML files and verify them. Even if they are in multiple subdirectories.
Another online XML Schema (XSD) validator: http://www.utilities-online.info/xsdvalidation/.
I'm just learning Schema. I'm using RELAX NG and using xmllint to validate. I'm getting frustrated by the errors coming out of xmlllint. I wish they were a little more informative.
If there is a wrong attribute in the XML then xmllint tells you the name of the unsupported attribute. But if you are missing an attribute in the XML you just get a message saying the element can not be validated.
I'm working on some very complicated XML with very complicated rules, and I'm new to this so tracking down which attribute is missing is taking a long time.
Update: I just found a java tool I'm liking a lot. It can be run from the command line like xmllint and it supports RELAX NG: https://msv.dev.java.net/
(Be sure to check the " Validate against external XML schema" Box)
I use Xerces:
An XML editor for quick and easy XML validation is available at http://www.xml-buddy.com
You just need to run the installer and after that you can validate your XML files with an easy to use desktop application or the command-line. In addition you also get support for Schematron and RelaxNG. Batch validation is also supported...
Update 1/13/2012: The command line tool is free to use and uses Xerces as XML parser.
(Be sure to check the " Validate against external XML schema" Box)
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
one great visual tool to validate and generate XSD from XML is IntelliJ IDEA, intuitive and simple.
You can connect your XML schema to Microsoft Visual Studio's Intellisense. This option gives you both real-time validation AND autocomplete, which is just awesome.
I have this exact scenario running on my free copy of Microsoft Visual C++ 2010 Express.
I found this online validator from 'corefiling' quite useful -
http://www.corefiling.com/opensource/schemaValidate.html
After trying few tools to validate my xsd, this is the one which gave me detailed error info - so I was able to fix the error in schema.
The online XML Schema Validator from DecisionSoft allows you to check an XML file against a given schema.
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
Source: Stackoverflow.com