274

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.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Jason Dagit
  • 13,684
  • 8
  • 33
  • 56
  • 155
    I love how many of these "not constructive" questions are top Google search results of things I search for and the answers are useful – matt freake Mar 07 '14 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 '14 at 19:04
  • 26
    this is the exact case where admins are being counter productive – fIwJlxSzApHEZIl Mar 24 '14 at 20:40
  • It maybe worth to take a look into a [similar question](http://stackoverflow.com/questions/1527847/xml-dtd-schema-validation-maven-plugin) in the context of the [Maven](http://maven.apache.org/what-is-maven.html) building tool. – Alberto Jul 21 '14 at 07:10
  • Try https://codverter.com/src/xmltoxsd – Jonathan Applebaum May 16 '19 at 15:18

14 Answers14

250

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.

Community
  • 1
  • 1
Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • 1
    I found the following helpful: http://jmvanel.free.fr/xsd/README.html – Matthew Hegarty Dec 02 '09 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 09 '13 at 08:38
  • 3
    Is there a way to ask a program to use xsi:schemaLocation instead of feeding all XSDs manually? – Gabor Garami Jul 11 '14 at 12:31
  • @GaborGarami Not at the moment, but it is a feature request: https://github.com/amouat/xsd-validator/issues/5 – Adrian Mouat Jul 23 '14 at 10:07
  • 2
    `xmllint` is not XML regex compliant, unfortunately, so some valid XML regexes will not pass through it – Sergey Bushmanov Dec 14 '16 at 19:07
  • Your blog post link returns a VERY suspicious PDF that only contains links to fishy Russian domains. – ColinMaudry Aug 21 '18 at 15:42
  • @ColinMaudry Which link?! If it's http://www.adrianmouat.com/bit-bucket/2013/11/xml-schema-validation/ there shouldn't be any PDFs or dodgy domains. It could be that a link in-between you and the site has been hacked because I still haven't moved to https. I'll look into it, but I suggest you also check other http domains to make sure it isn't your router. – Adrian Mouat Aug 22 '18 at 16:00
  • @AdrianMouat It's fine now. I didn't have trouble with any other http site, but I'll be careful. – ColinMaudry Aug 23 '18 at 16:43
  • @ColinMaudry Ok. I _have_ to move to HTTPS anyway. I'm embarrassed I've not done it yet! – Adrian Mouat Aug 24 '18 at 13:43
  • Maybe you could mention that the C++ Xerces version supports preparsed XSD files. For performance reasons the XSD files can be parsed at build time and be included inside the executable. For details see "Loading the XSD from a precompiled binary" in my answer https://stackoverflow.com/a/52464355/757777 – Erik Sjölund Sep 23 '18 at 08:59
116

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.

Derek Mahar
  • 27,608
  • 43
  • 124
  • 174
Nate
  • 18,892
  • 27
  • 70
  • 93
  • 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 Aug 10 '10 at 23:27
  • 5
    -1, this plugin is a pain to install – jcollum Sep 28 '10 at 21:06
  • 2
    I tested with npp 5.8.6, XMLTools 2.3.1 unicode beta2 r805, it works fine. – Palani Jan 28 '11 at 14:55
  • 5
    Great plugin. See [Notepad++ XmlTools Not Installing](http://stackoverflow.com/questions/2274154/notepad-xmltools-not-installing) for help with installation. – ladenedge Apr 20 '11 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 '11 at 11:54
  • **XmlTools**, as of now, can not validate XML against an embedded .NET-style XSD schema. There are 2 versions of embedding xsd into xml and XmlTools does not support .NET style! – Achilles Sep 28 '11 at 09:11
  • and this XML validation thingy is where NP++ owns the Sublime Text 2 – Achilles Sep 28 '11 at 09:14
  • can anyone tell me what to do if the "set xsd"- dialogue is not showed? – Franz Ebner Oct 24 '12 at 09:25
  • 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 '13 at 19:42
  • 1
    Note: Notepad++ writhes in pain when fed a 180MB XML file such as the one I'm trying to validate. – user1454265 Apr 16 '14 at 14:11
  • Could install this plugin via the built-in Plugin Manager tool. Notepad++ prompts you to restart the editor once and after that it just works. No need to do anything manually – TorbenJ Jun 21 '16 at 06:56
  • 1
    From notepad++ 7.5.1, the plugin manager is not installed by default, and you can follow the instruction here https://notepad-plus-plus.org/community/topic/14496/no-plugin-manager to download plugin manager. – zhihong Jan 22 '18 at 11:36
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
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
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

SteveC
  • 15,808
  • 23
  • 102
  • 173
4

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

3

I use Xerces:

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

John
  • 15,990
  • 10
  • 70
  • 110
  • 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? – Alexis Wilke Dec 29 '13 at 01: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 '20 at 21:42
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.

inutan
  • 10,558
  • 27
  • 84
  • 126
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.

Clemens
  • 1,744
  • 11
  • 20
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/

Pengo
  • 470
  • 1
  • 5
  • 17
1

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

jacktrade
  • 3,125
  • 2
  • 36
  • 50
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.

AaronDanielson
  • 2,230
  • 28
  • 29
1

http://www.xmlvalidation.com/

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

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.

Andrew Stern
  • 688
  • 9
  • 18
0

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