use*_*605 5 linux shell command-line xsd xmllint
命令行 xmllint --schema 验证失败,但 $? 返回 0
我的输入.xml:
<myinput><header>mytestvalue</header></myinput>
Run Code Online (Sandbox Code Playgroud)
myschema.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="myinput" type="xsd:string"/>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)
命令:
$xmllint --schema myschema.xsd myinput.xml
Run Code Online (Sandbox Code Playgroud)
结果:
Element myinput: child header should not be present
myinput.xml fails to validate
Run Code Online (Sandbox Code Playgroud)
命令:
$echo $?
Run Code Online (Sandbox Code Playgroud)
结果:
0
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么 xmllint 模式验证失败不会作为错误返回吗?或者建议我如何在我的 shell 脚本中将其捕获为错误?在我的 shell 脚本中,当前我正在“if”块中验证上述 xmllint 命令,它仅在 xml 格式良好时失败,但在模式验证失败时成功。
如果以上没有作为错误返回,我是否应该在 xmllint 输出上执行“grep 失败”的丑陋方式来确定模式验证是成功还是失败?有什么想法吗?
除了我在问题中提到的“AWK”ward 方法之外,似乎没有更好的方法了。无论如何,这就是我正在解决的方法:
$xmllint --noout --schema myschema.xsd myinput.xml >> $tmpFile
schemaResult=$(cat $tmpFile | grep myinput.xml | awk '{ print $2 }')
if [ "x$schemaResult" = "xvalidates" ];
echo "Schema validation succeeded"
else
echo "Schema validation failed"
fi
Run Code Online (Sandbox Code Playgroud)