275

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.

5
  • 160
    I love how many of these "not constructive" questions are top Google search results of things I search for and the answers are useful Mar 7, 2014 at 12:12
  • 2
    I understand where there coming from with the not constructive. Honestly, though this is a great question and debate/arguments sometimes end up deriving into great answers
    – Matt
    Mar 15, 2014 at 19:04
  • 26
    this is the exact case where admins are being counter productive Mar 24, 2014 at 20:40
  • It maybe worth to take a look into a similar question in the context of the Maven building tool.
    – Alberto
    Jul 21, 2014 at 7:10
  • Try codverter.com/src/xmltoxsd May 16, 2019 at 15:18

14 Answers 14

251

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.

10
  • 1
    I found the following helpful: jmvanel.free.fr/xsd/README.html Dec 2, 2009 at 11:10
  • An example StdinParse usage (on linux) migth be useful: StdInParse -n -s -f -v=always < XML_FILE . XML_FILE should include schema location. For me it gives more complete results than xmllint.
    – helcim
    Jul 9, 2013 at 8:38
  • 3
    Is there a way to ask a program to use xsi:schemaLocation instead of feeding all XSDs manually? Jul 11, 2014 at 12:31
  • @GaborGarami Not at the moment, but it is a feature request: github.com/amouat/xsd-validator/issues/5 Jul 23, 2014 at 10:07
  • 2
    xmllint is not XML regex compliant, unfortunately, so some valid XML regexes will not pass through it Dec 14, 2016 at 19:07
117

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.

12
  • 5
    -1, this plugin is a pain to install
    – jcollum
    Sep 28, 2010 at 21:06
  • 2
    I tested with npp 5.8.6, XMLTools 2.3.1 unicode beta2 r805, it works fine.
    – Palani
    Jan 28, 2011 at 14:55
  • 5
    Great plugin. See Notepad++ XmlTools Not Installing for help with installation.
    – ladenedge
    Apr 20, 2011 at 15:04
  • 17
    And with version 5.8.7, Notepad++'s own Plugin Manager ("Plugins/Plugin Manager") makes the installation process automatic.
    – Fueled
    Aug 22, 2011 at 11:54
  • 1
    @FranzEbner I had this same problem. If the XML itself references an XSD file, for example xsi:noNamespaceSchemaLocation="file.xsd", then XML Tools will try to use it instead of giving you a "set" dialogue.
    – theblang
    Jul 24, 2013 at 19:42
24

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
5
  • 3
    Do you know how serious the warning is about not fully supporting xml schemas? Sep 24, 2008 at 2:43
  • 2
    Judging by other answers xmlstarlet is a bit weak on its xsd implementation. Sep 24, 2008 at 22:54
  • 3
    Given xmlstarlet just uses libxml2 under the hood, it's probably easier to just use xmllint which is bundled with libxml2 and explained in the accepted answer. Nov 20, 2013 at 22:56
  • @JasonDagit, ...it's exactly as weak as xmllint is -- they use the same library implementation. Nov 8, 2017 at 13:49
  • The content model is not determinist. :-(
    – Greg
    Mar 7, 2018 at 21:10
17

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

4

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

3
3

I use Xerces:

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

2
  • 1
    Do they have a command line that can be used from a console to check the validation of an XSLT file against the XSD of XSLT 2.0? Dec 29, 2013 at 1:14
  • Yes, you can install a CLI tool with sudo apt install libxerces-c-samples and then check the document with StdInParse -v=always -n -s < document.xml.
    – jgrocha
    Apr 10, 2020 at 21:42
2

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/

2

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.

1
  • Online tools are a security issue.
    – Dragas
    Dec 7, 2021 at 14:57
2

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.

1

http://www.xmlvalidation.com/

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

1
  • @jcollum It seems to now.
    – recursive
    Nov 28, 2018 at 1:31
1

one great visual tool to validate and generate XSD from XML is IntelliJ IDEA, intuitive and simple.

1

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.

0

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.

0

Another online XML Schema (XSD) validator: http://www.utilities-online.info/xsdvalidation/.

Not the answer you're looking for? Browse other questions tagged or ask your own question.