Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查XML是否为;“格式良好”;在shell脚本中_Xml_Shell_Scripting - Fatal编程技术网

如何检查XML是否为;“格式良好”;在shell脚本中

如何检查XML是否为;“格式良好”;在shell脚本中,xml,shell,scripting,Xml,Shell,Scripting,我不熟悉shell脚本 要求: <heading>Reminder</heading> <body>Don't forget me this weekend!</body> <heading>Reminder</head> <body>Don't forget me this weekend!</> valid_xml.xml:2: validity error : Validation fai

我不熟悉shell脚本

要求:

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 
<heading>Reminder</head>
<body>Don't forget me this weekend!</>
valid_xml.xml:2: validity error : Validation failed: no DTD found !
<?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>
我想检查XML的格式是否正确。 我没有一个模式或其他东西来验证它。我只是想检查一下它的格式是否正确

为我提供正确XML的示例:

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 
<heading>Reminder</head>
<body>Don't forget me this weekend!</>
valid_xml.xml:2: validity error : Validation failed: no DTD found !
<?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>
我收到的错误:

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 
<heading>Reminder</head>
<body>Don't forget me this weekend!</>
valid_xml.xml:2: validity error : Validation failed: no DTD found !
<?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>
我使用的XML出错:

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 
<heading>Reminder</head>
<body>Don't forget me this weekend!</>
valid_xml.xml:2: validity error : Validation failed: no DTD found !
<?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,它将失败并显示错误消息(
验证失败:未找到DTD!

要检查xml文档是否“格式良好”,请使用以下命令

if xmllint filename.xml > /dev/null ; then
    echo "Valid"
else
    echo "Fail"
fi
请注意,OP提供的示例文件(最后一个示例除外)都不是格式良好的。标记为correct的示例文件缺少顶级XML标记。应该是:

<root>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 
</root>

提醒
这个周末别忘了我!

您实际查找的术语是(无效)