3

I want to get value of conn of abc whose name is 3 i.e conn3

<abc name="1">
    <properties conn="conn1"/>
</abc>
<abc name="2">
    <properties conn="conn2"/>
</abc>
<abc name="3">
    <properties conn="conn3"/>
</abc>

currently i am doing

echo 'cat //abc/properties/@conn' | xmllint --shell "test.xml"

but it is returning conn1,conn2,conn3

I am trying with

echo 'cat //abc[@name='1']/properties/@conn' | xmllint --shell "test.xml"

but it is not returning anything

Can you please suggest where i am doing wrong. Note:Xpath not supported

Thomas Dickey
  • 47,166
  • 7
  • 60
  • 95
user2387280
  • 85
  • 2
  • 11

2 Answers2

1

Finally issue was with single inverted comma(') below command worked for me. I don't know the reason, came to know by hit and try :)

Please comment if you know the reason behind it.

echo 'cat //abc[@name="1"]/properties/@conn' | xmllint --shell "test.xml"

Note: Above mentioned XML is only sample actual XML where I want to run is complex structure.

ggorlen
  • 33,459
  • 6
  • 59
  • 67
user2387280
  • 85
  • 2
  • 11
0

Just use xmllint normally, without pipes:

$ xmllint --xpath 'string(//abc[@name='1']/properties/@conn)' xml_file
conn1

See string() is used to get the value of an attribute, as described in Getting attribute using XPath.

Community
  • 1
  • 1
fedorqui
  • 252,262
  • 96
  • 511
  • 570