Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get xmllint to output xpath results \n-separated, for attribute selector

Tags:

How can I get xmllint to output multiple results of xpath selector for attributes "per line"?

Take this example:

  <?xml version="1.0" encoding="ISO-8859-1"?>   <config>           <tagX key1="value1 " key2=" value2"/>           <tagY key3="value3" key4=" value4 "/>   </config>     $ xmllint example.xml --xpath "/config/*/@*" 

The result is:

   key1="value1 " key2=" value2" key3="value3" key4=" value4 " 

What I'd like to get is:

   key1="value1 "    key2=" value2"    key3="value3"    key4=" value4 " 

Would I need to split after even-numbered quote marks, or is there any neater way to do this?

There's a related question, about the same subject except it's about picking out contents of <tag>value</tag>, and not <tag attribute="value" />

like image 578
SF. Avatar asked Jul 31 '13 08:07

SF.


2 Answers

You can try:

$ xmllint --shell inputfile <<< 'cat /config/*/@*' 

You might need to grep the output, though, so as to filter the undesired lines.

like image 188
devnull Avatar answered Sep 25 '22 13:09

devnull


If it's an option, try using xmlstarlet instead:

xmlstarlet sel -t -v "/config/*/@*" example.xml

like image 39
tremby Avatar answered Sep 24 '22 13:09

tremby