简体   繁体   English

Windows的命令行XML验证程序

[英]Command-line XML validator for Windows

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. 我总是发现针对模式的验证是对thinkos的一个无价之宝,并希望将验证检查作为项目的一部分,我经常需要手工编写几百行的XML文件。 My text editor has a fairly nice CLI integration feature, so I'm looking for a command-line validator. 我的文本编辑器有一个相当不错的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; libxml (通过cygwin) - 不报告行号; I have no idea where my errors are! 我不知道我的错误在哪里
  • msxml — cannot be run from the command line? msxml - 无法从命令行运行?
  • xerces-c — seems to require a copy of Visual C? xerces-c - 似乎需要Visual C的副本?
  • xerces2-j — cannot be run from the command line? xerces2-j - 无法从命令行运行?
  • xmlstarlet — insufficient XSD support* 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 xmlstarlet to 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) 在Win32上运行(特别是Windows XP SP3)
  • command-line; 命令行; my editor can work with just about any combination of stdin/-out/-err, arguments, temp files, etc. 我的编辑器可以使用stdin / -out / -err,参数,临时文件等的任何组合。
  • reasonably complete XSD support (particularly namespaces and substitution groups) 合理完成XSD支持(特别是命名空间和替换组)
  • reports the line number where the error occurred! 报告错误发生的行号!

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。

Your first option, xmllint (libxml2), does give line numbers for errors in the xml (and also in the xsd). 您的第一个选项xmllint(libxml2)确实为xml(以及xsd)中的错误提供了行号。 You probably just need a later version. 您可能只需要更高版本。 I just confirmed both using my copy, which is: 我刚用我的副本确认了两个,这是:

>  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 (eg " <invalidXml.xsd> "), but this doesn't seem to affect your task. 注意:我注意到xmllint将接受它不应该的元素名称(例如“ <invalidXml.xsd> ”),但这似乎不会影响您的任务。

EDIT adding 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 

As 13ren stated above, libxml's xmllint does report line numbers - perhaps you have a version issue. 正如13ren所述,libxml的xmllint会报告行号 - 也许您有版本问题。 You might find it useful to grab native (non cygwin) versions of the libxml/libxslt tools from http://www.zlatkovic.com/libxml.en.html 您可能会发现从http://www.zlatkovic.com/libxml.en.html获取libxml / libxslt工具的本机(非cygwin)版本很有用

You might also want to take a look at msv from Sun . 您可能还想看看Sun的 msv。 It isn't a full implementation of XSD but might do the job (I use it for RelaxNG validation generally) 它不是XSD的完整实现,但可以完成这项工作(我一般用它来进行RelaxNG验证)

I would suggest Windows Powershell with PowerShell Community eXtensions . 我建议使用PowerShell社区扩展的 Windows Powershell PSCX has the Test-Xml cmdlet which has the following Get-Help detailed description: PSCX具有Test-Xml cmdlet,其中包含以下Get-Help详细说明:

Tests for well formedness and optionally validates against XML Schema. 测试良构性并可选择验证XML Schema。 It doesn't handle specifying the targetName space. 它不处理指定targetName空间。 To see validation error messages, specify the -Verbose flag. 要查看验证错误消息,请指定-Verbose标志。

I do not know if it reports the errors with linenumbers but 3 out 4 isn't bad. 我不知道它是否报告了亚麻布的错误,但是3分4也不错。

You might try one of the Visual Studio 2008 Express editions. 您可以尝试使用Visual Studio 2008 Express版本之一。 There's much better XML support now, including validation, of course, but also XML Intellisense, XML snippets, and an XML Schema view. 现在有更好的XML支持,包括验证,当然还有XML Intellisense,XML片段和XML Schema视图。

无法评论,但Igor Zlatkovic的最新版xmllint(20708)Windows端口也提供了行号。

Xerces-J comes with a sample application, jaxp.SourceValidator . Xerces-J附带了一个示例应用程序jaxp.SourceValidator You can feed it your XML file and it will validate it. 您可以将它提供给您的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 runtime files. 至于Xerces-C,我自己没有使用它,但我知道它不需要所有的Visual C ++,它只需要运行时文件。 These can be downloaded separately from Microsoft. 这些可以从Microsoft单独下载。 There seems to be a sample application which does what you need - see StdInParse 似乎有一个示例应用程序可以满足您的需求 - 请参阅StdInParse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM