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

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

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


You can try:

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

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


The question is old but as I came to this post searching a solution to the same problem, here is my solution

On linux add sed substitution to split output:

$ xmllint example.xml --xpath "/config/*/@*" | sed "s| key|\nkey|g"

of course the substitution expression depends on your xml structure and your xpath query.

And you can even add line numbers on each line if you add nl

$ xmllint example.xml --xpath "/config/*/@*" | sed "s| key|\nkey|g" | nl

Which gives

   1 key1="value1 "
   2 key2=" value2"
   3 key3="value3"
   4 key4=" value4 "

If using the latest xmllint, results should have been separated by \n now. However, if fields contain \n, you can use this patched version to use \0 as a separator, with --xpath0. For whatever reason the PR hasn't been merged yet.

Tags:

Xml

Shell

Parsing