2

I asked a question earlier and it was closed as being a duplicate: How to parse XML to CSV with a shell script?. The answer in the duplicate that was linked suggested using a tool called XMLStarlet, but I'm unsure how to use this tool on OS X.

Additionally, there was a link to another Q&A titled: "Text between two tags" which shows examples using xslt? man xlst doesn't work but I looked more into it and it seems to be some type of Perl script-thing? I found this on StackOverflow, which is leading me to this conclusion.

Can someone please provide me with some guidance on how I can simply convert an XML file to CSV on OS X?

5
  • The tool that was suggested in the A'ers is XMLStarlet: xmlstar.sourceforge.net. That's different than XSLT. XSLT files are XML stylesheet templates.
    – slm
    Oct 25, 2014 at 1:05
  • This blog post shows how to install XMLStarlet for OSX: blog.troyastle.com/2010/03/…
    – slm
    Oct 25, 2014 at 1:07
  • The comment doesn't explain how to use it, I have no idea what a xml template is, where to put it or how to use it. Oct 25, 2014 at 5:34
  • Here's an example, hope this shows you how to install and use xmlstarlet.
    – slm
    Oct 25, 2014 at 8:00
  • Since there is no example XML document in the question, an answer would by necessity need to be extremely general, to the degree of being mostly unusable.
    – Kusalananda
    Jul 14, 2021 at 13:26

1 Answer 1

5

The application XMLStarlet appears to be available on OSX via brew, so you should be able to install it like so:

$ brew install xmlstarlet

Once it's installed, you use it via the command line via the command xmlstarlet.

Usage

$ xmlstarlet 
XMLStarlet Toolkit: Command line utilities for XML
Usage: xmlstarlet [<options>] <command> [<cmd-options>]
where <command> is one of:
  ed    (or edit)      - Edit/Update XML document(s)
  sel   (or select)    - Select data or query XML document(s) (XPATH, etc)
  tr    (or transform) - Transform XML document(s) using XSLT
  val   (or validate)  - Validate XML document(s) (well-formed/DTD/XSD/RelaxNG)
  fo    (or format)    - Format XML document(s)
  el    (or elements)  - Display element structure of XML document
  c14n  (or canonic)   - XML canonicalization
  ls    (or list)      - List directory as XML
  esc   (or escape)    - Escape special XML characters
  unesc (or unescape)  - Unescape special XML characters
  pyx   (or xmln)      - Convert XML into PYX format (based on ESIS - ISO 8879)
  p2x   (or depyx)     - Convert PYX into XML
<options> are:
  -q or --quiet        - no error output
  --doc-namespace      - extract namespace bindings from input doc (default)
  --no-doc-namespace   - don't extract namespace bindings from input doc
  --version            - show version
  --help               - show help
Wherever file name mentioned in command help it is assumed
that URL can be used instead as well.

Type: xmlstarlet <command> --help <ENTER> for command help

XMLStarlet is a command line toolkit to query/edit/check/transform
XML documents (for more information see http://xmlstar.sourceforge.net/)

Example

Say you had this sample file, sample.xml.

$ cat sample.xml 
<root>
  <record id="1">
    <keyA>val_1A</keyA>
    <keyB>val_1B</keyB>
  </record>
  <record id="2">
    <keyA>val_2A</keyA>
    <keyB>val_2B</keyB>
  </record>
  <record id="3">
    <keyA>val_3A</keyA>
    <keyB>val_3B</keyB>
  </record>
</root>

To parse this file into CSV, for each record, (1, 2, 3), with each records corresponding values, (keyA, keyB), on a single line you could use the concat command like so:

$ xmlstarlet \
    sel -T -t -m /root/record \
    -v "concat(@id,',',keyA,',',keyB)" \
    -n sample.xml

Which would results in the following output:

1,val_1A,val_1B
2,val_2A,val_2B
3,val_3A,val_3B

The workhouse line in the above command is the concat() function. This is taking the elements from the XML record, /root/record.

References

1
  • This is not a robust CSV solution. It fails if the input data includes commas. Aug 29, 2016 at 17:37

You must log in to answer this question.

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