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.. – roaima Jul 7 '16 at 10:30
  • Does the solution have to use xmlstarlet or can any suitable tool be used? – roaima Jul 7 '16 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 '16 at 10:54

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,

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 ,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.