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

4
  • 1
    Given that XML (and wrapping it in a <root> element to make it valid), your second command works for me. I guess what you have is something different? Commented Aug 10, 2015 at 14:10
  • Works for me, too. Although if you don't have the --xpath option, I wonder if your xmllint is old and doesn't support the particular query you're using... Commented Aug 10, 2015 at 14:11
  • Printing "/ > / >" to the console Commented Aug 10, 2015 at 14:15
  • What is the expected output, exactly? Related for siblings: stackoverflow.com/questions/26823736/… Commented Nov 19, 2015 at 5:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Hi, --xpath not supported :(
What does your xmllint --version return? Mine xmllint: using libxml version 20901.
xmllint: using libxml version 20706 I am using linux box xmllint --xpath Unknown option --xpath Usage : xmllint [options] XMLfiles .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.