如何验证 xml 文件在 sh 或 bash 中是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22099708/
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
How to verify whether a xml file is valid in sh or bash?
提问by user2799603
How to verify whether a XML file is valid in sh (preferably) or bash?
如何验证 XML 文件在 sh(最好)或 bash 中是否有效?
I have a file that often get corrupted and needs to be replaced while I take my time to investigate the underlying issue.
我有一个文件经常被损坏,需要更换,同时我花时间调查潜在问题。
Is there any easy way of performing this task with sh or bash?
有没有什么简单的方法可以用 sh 或 bash 执行这个任务?
回答by sanmiguel
Not directly with bash, but xmllint
is fairly widely available.
不直接使用 bash,但xmllint
相当广泛可用。
xmllint --format "${xmlfile}"
This will exit with non-zero status (hint: $?
in bash gets you the exit code of the last command) if the xml file is invalid.
$?
如果 xml 文件无效,这将以非零状态退出(提示:在 bash 中获取最后一个命令的退出代码)。
回答by Charles Duffy
XMLStarlethas a validate subcommand. At its simplest, to check for well-formedness:
XMLStarlet有一个 validate 子命令。最简单的方法是检查格式是否正确:
xmlstarlet val "$filename"
To validate against a DTD:
要根据 DTD 进行验证:
xmlstarlet val -d "$dtd_filename" "$xml_filename"
To validate against an XSD schema:
要针对 XSD 架构进行验证:
xmlstarlet val -s "$xsd_filename" "$xml_filename"
To validate against a RelaxNG schema:
要针对 RelaxNG 架构进行验证:
xmlstarlet val -r "$rng_filename" "$xml_filename"
This isn't built into bash -- bash has no built-in XML parser, and validation cannot be performed without one -- but it is widely packaged for modern OS distributions.
这不是内置在 bash 中的——bash 没有内置的 XML 解析器,没有一个就无法执行验证——但它被广泛打包用于现代操作系统发行版。
XMLStarlet also has subcommands for extracting information from XML files, editing XML files, etc. If you're going to be working with XML from shell scripts, its use is well-advised.
XMLStarlet 还具有用于从 XML 文件中提取信息、编辑 XML 文件等的子命令。如果您打算从 shell 脚本使用 XML,那么建议您使用它。
回答by emchateau
If you want to validate against a RelaxNG schema, which is an alternative grammar to W3C XML schema, you can use Libxml2(xmllint) but it only supports the RelaxNG XML syntax.
如果您想针对作为W3C XML 架构的替代语法的RelaxNG 架构进行验证,您可以使用Libxml2(xmllint),但它仅支持 RelaxNG XML 语法。
To validate an XML file with Libxml2against a RelaxNG schema
使用Libxml2针对 RelaxNG 架构验证 XML 文件
xmllint --noout --relaxng schema.rng file.xml
It is possible to convert a RelaxNG schema from compact syntax to XML syntaxt with trang. But you may also use Jingto To validate against a RelaxNG XML schema.
可以使用trang将 RelaxNG 模式从紧凑语法转换为 XML 语法。但是您也可以使用Jing来针对 RelaxNG XML 模式进行验证。
With jing installed on your computer, you can validate a file file.xml
against a schema schema.relaxng
like this?:
在您的计算机上安装 jing 后,您可以file.xml
根据这样的模式验证文件schema.relaxng
?:
jing schema.rng file.xml
To use the RelaxNG compact syntax?:
要使用 RelaxNG 紧凑语法?:
jing -c schema.rnc file.xml
回答by keshlam
Most parsers come with sample programs that can be run from the command line. Run one of those which validates the document.
大多数解析器都带有可以从命令行运行的示例程序。运行其中一个验证文档。
There are many good tools. As someone who has implemented several of them, but who cares more about the language than the specific tool you use, I decline to recommend one over another. If you insist on an answer to the question as posed, it's "you can't do that in sh or bash per se... at least not unless you are enough of a masochist to try to write it from the ground up, and then performance will be awful."
有很多很好的工具。作为已经实现了其中几个但更关心语言而不是您使用的特定工具的人,我拒绝推荐一个。如果您坚持要回答所提出的问题,那就是“您不能在 sh 或 bash 本身中做到这一点……至少不能,除非您是一个受虐狂,可以尝试从头开始编写它,并且那么表现会很糟糕。”