To answer your first question, The unquoted use of $ITEMS with echo is eliminating your new-line chars. Try
ITEMS=`echo "cat //title" | xmllint --shell rss.xml `
echo "$ITEMS" > tmpfile
#----^------^--- dbl-quotes only
In general, using for loops is best left to items that won't generate unexpected spaces or other non-printable characters. (non-alphanumerics), like for i in {1..10} ; do echo $i; done
AND you don't really need the variables, or the tempfile, try
echo "cat //title" | xmllint --shell rss.xml |
while read line ; do
echo "$line"
done
Depending on what is in your rrs feed, you may also benefit from changing the default IFS (Internal Field Separator) that is used by the read cmd, try
while IFS= read line ....
# or
while IFS="\n" read line
# or
while IFS="\r\n" read line
I'm not sure what you're trying to achieve with echo "cat //title" | going into xmllint, so I'm leaving it as is. Is that an instruction to xmllint? or is it passed thru to create a header to the document? (Don't have xmllint to expermient with right now).
Also, you might want to look at reading rss feeds with awk, but it is rather low level.
I hope this helps.
for val in "$ITEMS"; do echo "$val"; donexmllint --xpath '//title/text()' rss.xml?