34
votes

I'm writing an XML schema (an XSD) to describe the format our partners should send us data in.

And I'm having a hard time finding a tool that can validate the XSD schema file that I have written.

The best way I have found so far is to first write an example input XML file and then try to validate that with the XSD. But that doesn't feel like a best practice maneuver.

So, how should I validate an XML schema?

5
Good question. Perhaps "validate" isn't the best verb here, as you "validate" an xml against a xsd, which is not you are asking about. But I have gone through the same process, used example input to check the xsd, and found myself later having to change the xsd because of corner cases that weren't tested for.Daniel C. Sobral
@Daniel, not quite. Quite a few tools exist to validate the schemas themselves including commercial tools; you can also build one based on the Apache Xerces libs.lavinio
Because some commercial editors are already mentioned, in the meantime there are also other tools available for a more reasonable price. For example XML ValidatorBuddy Has also support for XML batch validation.lichtfusion
Batch validation can help you a lot on testing your schemas and doing better regression tests also. So having a tool supporting some kind of batch tasks is definitely a benefit.Clemens
In Java I have successfully used XSOM as suggested hereAlberto

5 Answers

14
votes

The W3C has a online validator for XML Schemas at http://www.w3.org/2001/03/webdata/xsv. Since W3C is the source of the XML Schema spec, their validator should be trustworthy.

12
votes

If this is a short-term thing, you could use an evaluation copy of a tool like Stylus Studio.

If it's long-term maintenance, you might want to consider purchasing an XML schema editor like Stylus, or Oxygen or Altova.

You didn't specify the source language, but it's only a few lines of code to write a schema validator in Java or .Net.

8
votes

the Java SDK comes with a standard tool called xjc . This tool generates the classes parsing your schema. You could use this code to validate your partners' input.

See also : http://www.java2s.com/Tutorial/Java/0440__XML/ThexjcTool.htm

4
votes
cd /the/dir/with/your/schema
curl -O https://www.w3.org/2012/04/XMLSchema.xsd
xmllint.exe --noout --schema XMLSchema.xsd <your schema>

In *nix (including git-bash or similar on Windows), if the schema is valid then $? == 0 else $? == 1. I'm sure there is some powershell equivalent ...

This came from a comment by @AlexanderKjäll to an answer by @lavinio elsewhere here. I added my own comment to say @AlexanderKjäll should add this as an answer. However (for me atleast), it wasn't quite correct since it won't work using the remote file URI. And thus my answer. If you upvote this could you please upvote their comment.

1
votes

So, how should I validate an XML schema (XSD)?

The question you ask is a good one. Just "winging it" is likely to get you in trouble. From the Wikipedia article XML Schema Editor (emphasis mine):

The [W3C XML Schema] standard is versatile, allowing for programming concepts such as inheritance and type creation, but it is complex. The standard itself is highly technical and published in 3 different parts, making it difficult to understand without committing large amounts of time.

So, given the complexity of the W3C XML Schema standard, how do you ensure that a schema that you create complies with that standard? From the same Wikipedia article:

The problems users face when working with the XSD standard can be mitigated with the use of graphical editing tools. Although any text-based editor can be used to edit an XML Schema, a graphical editor offers advantages; allowing the structure of the document to be viewed graphically and edited with validation support, entry helpers and other useful features.

At the bottom of that Wikipedia article you'll find a list of XML schema editors, some of which are licensed as "free software."