使用 xmllint 从 xml 获取多次出现的属性值
Get value of an attribute from xml for multiple occurrences using xmllint
我想获取名称为 3 的 abc 的 conn 值,即 conn3
<abc name="1">
<properties conn="conn1"/>
</abc>
<abc name="2">
<properties conn="conn2"/>
</abc>
<abc name="3">
<properties conn="conn3"/>
</abc>
目前我在做
echo 'cat //abc/properties/@conn' | xmllint --shell "test.xml"
但它正在返回 conn1、conn2、conn3
我正在尝试
echo 'cat //abc[@name='1']/properties/@conn' | xmllint --shell "test.xml"
但它没有返回任何东西
你能告诉我哪里做错了吗?
Note:Xpath
不支持
正常使用xmllint
,不用管道:
$ xmllint --xpath 'string(//abc[@name='1']/properties/@conn)' xml_file
conn1
请参阅string()
用于获取属性值,如Getting attribute using XPath所述。
最后,问题是命令下方的单个引号 (') 对我有用。不知道是什么原因,一试就知道了:)
如果你知道背后的原因,请评论。
echo 'cat //abc[@name="1"]/properties/@conn' | xmllint --shell "test.xml"
注意:上面提到的 XML 只是示例实际 XML 我想要 运行 是复杂的结构。
我想获取名称为 3 的 abc 的 conn 值,即 conn3
<abc name="1">
<properties conn="conn1"/>
</abc>
<abc name="2">
<properties conn="conn2"/>
</abc>
<abc name="3">
<properties conn="conn3"/>
</abc>
目前我在做
echo 'cat //abc/properties/@conn' | xmllint --shell "test.xml"
但它正在返回 conn1、conn2、conn3
我正在尝试
echo 'cat //abc[@name='1']/properties/@conn' | xmllint --shell "test.xml"
但它没有返回任何东西
你能告诉我哪里做错了吗?
Note:Xpath
不支持
正常使用xmllint
,不用管道:
$ xmllint --xpath 'string(//abc[@name='1']/properties/@conn)' xml_file
conn1
请参阅string()
用于获取属性值,如Getting attribute using XPath所述。
最后,问题是命令下方的单个引号 (') 对我有用。不知道是什么原因,一试就知道了:)
如果你知道背后的原因,请评论。
echo 'cat //abc[@name="1"]/properties/@conn' | xmllint --shell "test.xml"
注意:上面提到的 XML 只是示例实际 XML 我想要 运行 是复杂的结构。