如何在 shell 脚本中检查 XML 是否为 "well formed"
How to check if XML is "well formed" in a shell script
我是 shell 脚本的新手。
要求:
我想检查 XML 是否格式正确。
我没有模式或其他东西来验证它。我只是想检查它是否格式正确。
对我来说 XML 的正确示例:
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
对我来说 XML 不正确:
<heading>Reminder</head>
<body>Don't forget me this weekend!</>
目前我尝试过的:
我写了一个shell脚本命令
xmllint --valid filename.xml // replaced the filename.xml with my file.
我得到的错误:
valid_xml.xml:2: validity error : Validation failed: no DTD found !
我使用的 XML 出现错误:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
当您使用 --valid
时 xmllint 将尝试验证 DTD,因为您没有 DTD,它将失败并显示错误消息 (Validation failed: no DTD found !
)。
要检查 xml 文档是否“格式正确”,请使用以下
if xmllint filename.xml > /dev/null ; then
echo "Valid"
else
echo "Fail"
fi
请注意,none OP 提供的示例文件(最后一个示例除外)格式正确。标记为正确的示例文件缺少顶级 XML 标记。应该是:
<root>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</root>
我是 shell 脚本的新手。
要求:
我想检查 XML 是否格式正确。 我没有模式或其他东西来验证它。我只是想检查它是否格式正确。
对我来说 XML 的正确示例:
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
对我来说 XML 不正确:
<heading>Reminder</head>
<body>Don't forget me this weekend!</>
目前我尝试过的:
我写了一个shell脚本命令
xmllint --valid filename.xml // replaced the filename.xml with my file.
我得到的错误:
valid_xml.xml:2: validity error : Validation failed: no DTD found !
我使用的 XML 出现错误:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
当您使用 --valid
时 xmllint 将尝试验证 DTD,因为您没有 DTD,它将失败并显示错误消息 (Validation failed: no DTD found !
)。
要检查 xml 文档是否“格式正确”,请使用以下
if xmllint filename.xml > /dev/null ; then
echo "Valid"
else
echo "Fail"
fi
请注意,none OP 提供的示例文件(最后一个示例除外)格式正确。标记为正确的示例文件缺少顶级 XML 标记。应该是:
<root>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</root>