Concat multiple xml elements with xmlstarlet

Viewed 6385

I am trying to write a bash script to extract the multiple "directors" from an xml file such as this and concat them separated by a pipe, i.e. Tom Tykwer|Andy Wachowski.

The relevant xml section is:

<directors>
<item>Tom Tykwer</item>
<item>Andy Wachowski</item>
</directors>

With xmlstarlet in a bash script the following commands:

DIRECTORS=$(xmlstarlet sel -t -v "imdbdocument/directors/item" mymoviexml)
echo $DIRECTORS

give me

Tom Tykwer Andy Wachowski

and this command directly at the terminal

xmlstarlet sel -t -v "imdbdocument/directors/item" mymovieapi.xml

gives me:

(empty line)
Tom Tykwer
Andy Wachowski

I don't know why the new lines are being added when I am not specifying the -n option.

A few of my searches have suggested something like this:

xmlstarlet sel -t -m "imdbdocument/directors" -v "item" -o "|" mymovieapi.xml 

but this just gives me:

Tom Tykwer
Andy Wachowski|

I'd appreciate any help I can get. I'm seeing this behaviour with xmlstarlet 1.3.1 on Debian Wheezy and xmlstarlet 1.5.0 on Xubuntu 13.10.

2 Answers
Related