XML Schema (XSD) 验证工具?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/124865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Schema (XSD) validation tool?
提问by Jason Dagit
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.
在办公室,我们目前正在编写一个应用程序,该应用程序将根据给定的模式生成 XML 文件。我们在 .XSD 文件中有架构。
Are there tool or libraries that we can use for automated testing to check that the generated XML matches the schema?
是否有工具或库可用于自动测试以检查生成的 XML 是否与模式匹配?
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.
如果这有什么不同,我们的开发语言是 C++,尽管我认为它不应该,因为我们可以生成 xml 文件,然后通过在测试中调用单独的程序来进行验证。
采纳答案by Adrian Mouat
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 githubto validate from the command line using the default JRE parser, which is normally Xerces. This can be used on Windows/Mac/Linux.
经过一些研究,我认为最好的答案是Xerces,因为它实现了所有的 XSD,是跨平台的并且被广泛使用。我在 github 上创建了一个小型 Java 项目,以使用默认的 JRE 解析器(通常是 Xerces)从命令行进行验证。这可以在 Windows/Mac/Linux 上使用。
There is also a C++ version of Xercesavailable if you'd rather use that. The StdInParse utilitycan be used to call it from the command line. Also, a commenter below points to this more complete wrapper utility.
如果您更愿意使用它,还有一个C++ 版本的 Xerces可用。所述StdInParse实用程序可用于在命令行调用它。此外,下面的评论者指出了这个更完整的包装器实用程序。
You could also use xmllint, which is part of libxml. You may well already have it installed. Example usage:
您还可以使用 xmllint,它是libxml 的一部分。您可能已经安装了它。用法示例:
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 :(
一个问题是 libxml 没有实现所有规范,所以你可能会遇到问题:(
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 2007as suggested by SteveC(there are also several commercial editors which I won't mention here).
或者,如果您使用的是 Windows,则可以使用msxml,但您需要某种包装器来调用它,例如这篇DDJ 文章 中描述的 GUI 。然而,似乎大多数人都在Windows上使用XML编辑器,如记事本++(如描述Nate的答案)或XML记事本2007年作为由SteveC建议(也有,我不会在这里提几个商业编辑)。
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.
最后,不幸的是,您会发现不同的程序会给出不同的结果。这主要是由于 XSD 规范的复杂性。您可能希望使用多种工具测试您的架构。
UPDATE: I've expanded on this in a blog post.
更新:我在博客文章中对此进行了扩展。
回答by Nate
回答by Charles Duffy
xmlstarletis a command-line tool which will do this and more:
xmlstarlet是一个命令行工具,它将执行以下操作以及更多操作:
$ 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
回答by SteveC
For Windows there is the free XML Notepad 2007. You can select XSD's for it to validate against
对于 Windows,有免费的XML Notepad 2007。您可以为其选择 XSD 进行验证
UPDATE: better yet, use Notepad++ with the XML Tools plugin
更新:更好的是,使用 Notepad++ 和 XML 工具插件
回答by SteveC
The online XML Schema Validatorfrom DecisionSoft allows you to check an XML file against a given schema.
DecisionSoft的在线XML 模式验证器允许您根据给定的模式检查 XML 文件。
回答by John
回答by John
(Be sure to check the " Validate against external XML schema" Box)
(一定要选中“根据外部 XML 模式验证”框)
回答by Clemens
An XML editor for quick and easy XML validation is available at http://www.xml-buddy.com
可在http://www.xml-buddy.com上获得用于快速、轻松地进行 XML 验证的 XML 编辑器
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...
您只需要运行安装程序,然后您就可以使用易于使用的桌面应用程序或命令行来验证您的 XML 文件。此外,您还可以获得对 Schematron 和 RelaxNG 的支持。还支持批量验证...
Update 1/13/2012: The command line tool is free to use and uses Xerces as XML parser.
2012 年 1 月 13 日更新:命令行工具可以免费使用,并使用 Xerces 作为 XML 解析器。
回答by Pengo
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.
我只是在学习架构。我正在使用 RELAX NG 并使用 xmllint 进行验证。我对来自 xmlllint 的错误感到沮丧。我希望他们能提供更多信息。
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.
如果 XML 中有错误的属性,则 xmllint 会告诉您不受支持的属性的名称。但是,如果您在 XML 中缺少一个属性,您只会收到一条消息,指出无法验证该元素。
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.
我正在处理一些具有非常复杂规则的非常复杂的 XML,而且我是新手,因此跟踪缺少哪个属性需要很长时间。
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/
更新:我刚刚找到了一个我非常喜欢的 Java 工具。它可以像 xmllint 一样从命令行运行,并且支持 RELAX NG:https://msv.dev.java.net/
回答by inutan
I found this online validator from 'corefiling' quite useful -
http://www.corefiling.com/opensource/schemaValidate.html
我发现来自“corefiling”的这个在线验证器非常有用 -
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.
在尝试了一些工具来验证我的 xsd 之后,这是一个给我详细错误信息的工具 - 所以我能够修复架构中的错误。

