如何解决如何使用 xmllint 从 XML 中获取 mh 值和名称
有这样一个 XML 文件。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response" total-models="922" throttle="922" error="EndOfResults">
<model-responses>
<model mh="0x1058905">
<attribute id="0x1006e">prod-vpn-gw-v01.e-x.com</attribute>
</model>
<model mh="0x1058907">
<attribute id="0x1006e">prod-storage-san-z01-ssh.e-x.com</attribute>
</model>
<model mh="0x1058900">
<attribute id="0x1006e">test-vpn-gw-v01</attribute>
</model>
</model-responses>
</model-response-list>
我需要打印一个列表:
0x1058905 prod-vpn-gw-v01.e-x.com
0x1058907 prod-storage-san-z01-ssh.e-x.com
0x1058900 test-vpn-gw-v01
我尝试过:
xmllint --xpath "//[local-name()='model']/[local-name()='attribute']/text()" devices.xml>
但它仅用于名称,真的不知道如何将它与 an 一起使用并在其中获得 0x... mh 值。
有人可以帮忙吗? 谢谢。
解决方法
xmllint 不是这个的想法工具(它只支持 xpath 1.0),但如果你必须使用它,请尝试以下方法;它应该让你足够接近:
xmllint --xpath ".//*[local-name()='model']/@mh | .//*[local-name()='model']//*/text()" devices.xml
输出:
mh="0x1058905"
prod-vpn-gw-v01.e-x.com
mh="0x1058907"
prod-storage-san-z01-ssh.e-x.com
mh="0x1058900"
test-vpn-gw-v01
,
另一种选择是使用 xmlstarlet 匹配 model
元素,然后使用 concat() 输出所需的值...
xmlstarlet sel -t -m "//_:model" -v "concat(@mh,' ',_:attribute)" -n devices.xml
输出...
0x1058905 prod-vpn-gw-v01.e-x.com
0x1058907 prod-storage-san-z01-ssh.e-x.com
0x1058900 test-vpn-gw-v01
注意:我使用的是 1.6.1 版的 xmlstarlet。并非所有版本都支持“_”作为命名空间前缀。 (1.5.0+ 版本支持)
有关 xmlstarlet 的“sel”命令的更多信息,请参见 http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#idm47077139652416。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。