我想使用bash过滤example-below xml。从xml获取属性名称
问题: 如果子节点att =“t”的属性存在,则应该打印属性名称。
输入:
<?xml version="1.0" encoding="UTF-8"?>
<general>
<node1>
<subnode att1="a" att2="t" name="test"> </subnode>
<subnode att1="a" name="test2"> </subnode>
<subnode att1="a" name="test3"> </subnode>
<subnode att1="a" att2="t" name="test4"> </subnode>
</node1>
</general>
输出:
test
test4
我试图grep和xmllint,但没有成功。
我的 “临时” 的解决方案是:
xmllint --xpath 'string(//general/node1/subnode[@att2="t"]/@name)' file.xml
但该命令只打印第一次出现 - 测试。
不工作,有时打印其他属性。 – profiler
我需要非工作案例的XML内容,以便我可以修复。请更新您的问题更少的情况下。另外,我的代码假设你想匹配包含att [any_number] =“t”的行。不是吗?你只需要匹配att2 =“t”? – 2016-07-07 12:06:59