使用 xmllint 检查 XML 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11622995/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:36:50  来源:igfitidea点击:

check XML syntax with xmllint

xml

提问by user1547254

I am having a problem with some XML print files where the source system omits to convert some characters to their XML syntax equivalent (e.g. & is not converted to &).

我在处理某些 XML 打印文件时遇到问题,其中源系统忽略将某些字符转换为其等效的 XML 语法(例如 & 未转换为&)。

Is there a way to catch this with xmllint? (I don't need to check the general tree structure using XSD).

有没有办法用 xmllint 来捕捉这个?(我不需要使用 XSD 检查通用树结构)。

回答by Jarekczek

xmllint --noout your_test_file.xml

Check for return code of this command. See documentation. Value of 1is returned when basic parsing errors are met. E.g.:

检查此命令的返回码。请参阅文档1满足基本解析错误时返回的值。例如:

echo $?

回答by user3.1415

xmllint --valid --encode utf-8 TEST.xml

will validate and output TEST.xml in utf-8

将验证并输出 UTF-8 格式的 TEST.xml

cat TEST.xml

<xml version="1.0" encoding="utf-8"?>

<xml version="1.0" encoding="utf-8"?>

<!DOCTYPE JM SYSTEM "mydtd">

<!DOCTYPE JM 系统 "mydtd">

<JM> . . . </JM>

<JM>。. . </JM>

回答by dhirajforyou

If you just need to check validity(correctness) of any xml document using xmllint, here is one more way.

如果您只需要使用 xmllint 检查任何 xml 文档的有效性(正确性),这是另一种方法。

if xmllint --noout /tmp/test.xml > /dev/null 2>&1;
then
    echo "correct"
else
    echo "incorrect"
fi