命令行xmllint --schema验证失败,但$?返回0
myinput.xml:
<myinput><header>mytestvalue</header></myinput>
myschema.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="myinput" type="xsd:string"/>
</xsd:schema>
命令:
$xmllint --schema myschema.xsd myinput.xml
结果:
Element myinput: child header should not be present
myinput.xml fails to validate
命令:
$echo $?
结果:
0
有人能告诉我为什么xmllint架构验证失败不会作为错误返回吗?或者建议我在shell脚本中捕获这个错误的方法? 在我的shell脚本中,当前我在“if”块中验证上面的xmllint命令,它只对xml格式良好失败,但成功用于架构验证失败。
如果以上内容没有作为错误返回,我是否应该在xmllint输出上执行“grep failed”的丑陋方式,以确定架构验证是成功还是失败? 有什么想法吗?
答案 0 :(得分:0)
除了我在问题中提到的“AWK”病房之外,似乎没有更好的方法。无论如何,我的工作方式如下:
$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
答案 1 :(得分:0)
我想知道您正在使用的xml utils版本。
arturcz@szczaw:/tmp/stack$ xmllint --schema myschema.xsd myinput.xml; echo $?
<?xml version="1.0"?>
<myinput><header>mytestvalue</header></myinput>
myinput.xml:1: element myinput: Schemas validity error : Element 'myinput': Element content is not allowed, because the type definition is simple.
myinput.xml fails to validate
3
3是echo $?的结果。它适用于以下版本:
请原谅我,如果这对你来说很明显,但你需要记住,你不应该在xmllint call和echo $?之间运行任何命令。所以,试试我上面的确切调用 - 两个命令在一行中用分号分隔。通过这种方式,您可以验证xmllint是否真的有问题。
如果上面的消化对你没有帮助,请提供xmllint --version和你正在使用的shell的输出。