如何解决如何使用 xmllint
我正在使用 bash
和 xmllint
检查以下 xml 中的节点:
<?xml version="1.0" encoding="utf-8"?>
<output>
<document>
<sentence id="13">
<text>This is a test sentence.</text>
<entities>
<annotation id="3">
<grammar-form id="0" normal-form="THIS"/>
</annotation>
<annotation id="4">
<grammar-form id="0" normal-form="IS"/>
</annotation>
<annotation id="5">
<grammar-form id="0" normal-form="A"/>
</annotation>
<annotation id="6">
<grammar-form id="0" normal-form="TEST"/>
</annotation>
<annotation id="7">
<grammar-form id="0" normal-form="SENTENCE"/>
</annotation>
<annotation id="12">
<grammar-form id="0" normal-form="."/>
</annotation>
</entities>
</sentence>
</document>
</output>
如何简单地检查每个 grammar-form
节点是否存在 normal-form
属性?属性值是什么并不重要,我只需要检查它是否存在。
解决方法
选择没有属性的grammar-form
更容易,看看是否有任何匹配:
if xmllint --xpath '//grammar-form[not(@normal-form)]' input.xml 1>/dev/null 2>&1; then
echo "There are missing normal forms."
else
echo "There are no missing normal forms."
fi
在 xpath 模式下,xmllint
将打印匹配的路径,或者如果没有匹配的路径,则以错误代码 10 退出并打印一条消息到标准错误(中提到的 --noout
选项)不幸的是,用于抑制输出的手册页在我正在测试的版本中没有做任何事情),因此重定向。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。