The sample XML looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

The desired output looks like:

  • How important are the spaces in the comma-separated fields of your output? I don't see those spaces in your input file, and they're not consistent.. Jul 7, 2016 at 10:30
  • Does the solution have to use xmlstarlet or can any suitable tool be used? Jul 7, 2016 at 10:31
  • and why are some fields truncated in the output to 3 or 4 characters while others are full-length?
    – cas
    Jul 7, 2016 at 10:54

2 Answers 2


xmlstartlet arguments are a little bit tricky. You have to see them as templtes (-t) in the xsl way...

xmlstarlet sel -B -t -m '//text()' -c 'concat(.,",")' x1.xml


  • -B : generically remove spaces
  • -t : template in the xsl sense
  • -m : match xpath exp
  • -c : copy-of xpath exp

This expression, produces an extra ",". Naturally we can uses normal Unix tools to help:

xmlstarlet sel -B -t -v '//text()' x1.xml | 
    sed -z 's/\n/, /g; s/$/\n/'
  • -t : a template (in xsl sense)
  • -v : value-of (xpath expression)
  • sed... to trim ,

Using xml2 (available packaged for debian and most other distros) instead of xml2starlet, along with awk and paste:

$ xml2 <sdfsdf.xml | awk -F= '{ print $2 }' | paste -sd,

if you want spaces after each comma, add them with sed:

xml2 <sdfsdf.xml | awk -F= '{ print $2 }' | paste -sd, | sed -e 's/,/, /g'

cut can also work in place of awk but I'm guessing there's other criteria you haven't mentioned yet, so I'll stick with awk for now. Anyway, here's the cut version:

xml2 <sdfsdf.xml | cut -d= -f2 | paste -sd,

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .