适用于 Windows 的命令行 XML 验证器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1172662/
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
Command-line XML validator for Windows
提问by Ben Blank
I've always found validation against a schema to be an invaluable ward against thinkos and would like to incorporate validation checks as part of a project where I frequently need to hand-write XML files a few hundred lines in length. My text editor has a fairly nice CLI integration feature, so I'm looking for a command-line validator.
我一直发现针对模式的验证是对 thinkos 的宝贵保护,并且希望将验证检查作为项目的一部分,在该项目中我经常需要手写几百行长的 XML 文件。我的文本编辑器有一个相当不错的 CLI 集成功能,所以我正在寻找一个命令行验证器。
When I didn't find any clear winners via Google, I poked around here and found a similar question, but none of the tools suggested there quite fit my needs:
当我没有通过谷歌找到任何明显的赢家时,我在这里四处寻找并发现了一个类似的问题,但没有一个工具建议那里非常适合我的需求:
- libxml(via cygwin) — does not report line numbers; I have no idea wheremy errors are!
- msxml— cannot be run from the command line?
- xerces-c— seems to require a copy of Visual C?
- xerces2-j— cannot be run from the command line?
- xmlstarlet— insufficient XSD support*
- libxml(通过 cygwin)——不报告行号;我不知道我的错误在哪里!
- msxml— 不能从命令行运行?
- xerces-c— 似乎需要一份 Visual C 的副本?
- xerces2-j— 不能从命令行运行?
- xmlstarlet— XSD 支持不足*
(*The schema I'm validating against uses substitution groups — inappropriately, but it's external to the project, so I can't change it — which causes xmlstarletto choke even on valid files.)
(*我正在验证的模式使用替换组 - 不恰当,但它是项目的外部,所以我无法更改它 -xmlstarlet即使在有效文件上也会导致窒息。)
Normally, this is the point in solving a problem at which I'd give up on looking for an existing solution and reach for the Python-hammer, but Python's XML support is notoriously… well… actually, let's just leave it at "notorious".
通常,这是解决我放弃寻找现有解决方案并使用 Python-hammer 的问题的关键,但是 Python 的 XML 支持是出了名的......好吧......实际上,让我们把它留在“臭名昭著” .
So I'm back to looking for a pre-existing tool. My requirements are pretty simple:
所以我又开始寻找一个预先存在的工具。我的要求很简单:
- runs on Win32 (Windows XP SP3, specifically)
- command-line; my editor can work with just about any combination of stdin/-out/-err, arguments, temp files, etc.
- reasonably complete XSD support (particularly namespaces and substitution groups)
- reports the line number where the error occurred!
- 在 Win32 上运行(特别是 Windows XP SP3)
- 命令行; 我的编辑器几乎可以使用 stdin/-out/-err、参数、临时文件等的任意组合。
- 合理完整的 XSD 支持(特别是命名空间和替换组)
- 报告发生错误的行号!
Does such a tool exist? I'd prefer not to have to install Visual Studio and friends (too bloated, IMO), but I do already have both Cygwin and Python installed.
有这样的工具吗?我不想安装 Visual Studio 和朋友(太臃肿,IMO),但我已经安装了 Cygwin 和 Python。
采纳答案by 13ren
Your first option, xmllint (libxml2), does give line numbers for errors in the xml (and also in the xsd). You probably just need a later version. I just confirmed both using my copy, which is:
您的第一个选项 xmllint (libxml2) 确实为 xml(以及 xsd)中的错误提供了行号。您可能只需要更高版本。我刚刚使用我的副本确认了两者,即:
> xmllint --version
xmllint: using libxml version 20627
Example output:
示例输出:
invalidXml.xml:4: element c: Schemas validity error : Element 'c': This element is not expected. Expected is ( b ).
invalidXml.xml fails to validate
<?xml version="1.0"?>
<invalidXmlEg>
<a/>
<!-- <b></b> -->
<c/>
</invalidXmlEg>
Where the xsd is:
xsd 在哪里:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="invalidXmlEg">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:string" />
<xs:element name="b" type="xs:string" />
<xs:element name="c" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
NOTE: I have noticed that xmllint will accept elements names that it shouldn't (e.g. "<invalidXml.xsd>"), but this doesn't seem to affect your task.
注意:我注意到 xmllint 会接受它不应该接受的元素名称(例如“ <invalidXml.xsd>”),但这似乎不会影响您的任务。
EDITadding the "compiled with" part of the version:
编辑添加版本的“编译”部分:
compiled with: Threads Tree Output Push Reader
Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy
C14N Catalog XPath XPointer XInclude Iconv ISO8859X
Unicode Regexps Automata Expr Schemas Schematron
Modules Debug Zlib
回答by Nic Gibson
As 13ren stated above, libxml's xmllint does report line numbers - perhaps you have a version issue. You might find it useful to grab native (non cygwin) versions of the libxml/libxslt tools from http://www.zlatkovic.com/libxml.en.html
正如上面 13ren 所说,libxml 的 xmllint 确实报告了行号 - 也许您有版本问题。您可能会发现从http://www.zlatkovic.com/libxml.en.html获取本地(非 cygwin)版本的 libxml/libxslt 工具很有用
You might also want to take a look at msv from Sun. It isn't a full implementation of XSD but might do the job (I use it for RelaxNG validation generally)
您可能还想查看来自Sun 的msv 。它不是 XSD 的完整实现,但可以完成这项工作(我通常将它用于 RelaxNG 验证)
回答by Bas Bossink
I would suggest Windows Powershellwith PowerShell Community eXtensions. PSCX has the Test-Xml cmdlet which has the following Get-Helpdetailed description:
我建议使用带有PowerShell Community eXtensions 的Windows Powershell。PSCX 有 Test-Xml cmdlet,其Get-Help详细描述如下:
Tests for well formedness and optionally validates against XML Schema. It doesn't handle specifying the targetName space. To see validation error messages, specify the -Verbose flag.
测试格式是否正确,并可选择根据 XML 模式进行验证。它不处理指定 targetName 空间。要查看验证错误消息,请指定 -Verbose 标志。
I do not know if it reports the errors with linenumbers but 3 out 4 isn't bad.
我不知道它是否报告了 linenumbers 的错误,但 3 out 4 还不错。
回答by JReader
Unable to comment, but the latest version of xmllint (20708) Windows port from Igor Zlatkovic is giving line numbers as well.
无法发表评论,但来自 Igor Zlatkovic 的最新版本的 xmllint (20708) Windows 端口也提供了行号。
回答by John Saunders
You might try one of the Visual Studio 2008 Expresseditions. There's much better XML support now, including validation, of course, but also XML Intellisense, XML snippets, and an XML Schema view.
您可以尝试其中一种Visual Studio 2008 Express版本。现在有更好的 XML 支持,当然包括验证,还有 XML Intellisense、XML 片段和 XML 模式视图。
回答by Juris
Xerces-J comes with a sample application, jaxp.SourceValidator. You can feed it your XML file and it will validate it.
Xerces-J 附带了一个示例应用程序jaxp.SourceValidator。您可以将您的 XML 文件提供给它,它会对其进行验证。
As for Xerces-C, I haven't used it myself, but I know it does not require all of the Visual C++, all it needs is runtimefiles. These can be downloaded separately from Microsoft. There seems to be a sample application which does what you need - see StdInParse
至于 Xerces-C,我自己没有使用过,但我知道它不需要所有的 Visual C++,它只需要运行时文件。这些可以从 Microsoft 单独下载。似乎有一个示例应用程序可以满足您的需求 - 请参阅StdInParse

