Parsing XML using unix terminal Parsing XML using unix terminal xml xml

Parsing XML using unix terminal


Peter's answer is correct, but it outputs a trailing line feed.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  <xsl:output method="text"/>  <xsl:template match="root">    <xsl:for-each select="myel">      <xsl:value-of select="@name"/>      <xsl:text>,</xsl:text>      <xsl:if test="not(position() = last())">        <xsl:text>&#xA;</xsl:text>      </xsl:if>    </xsl:for-each>  </xsl:template></xsl:stylesheet>

Just run e.g.

xsltproc stylesheet.xsl source.xml

to generate the CSV results into standard output.


Use a command-line XSLT processor such as xsltproc, saxon or xalan to parse the XML and generate CSV. Here's an example, which for your case is the stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:output method="text"/>    <xsl:template match="root">        <xsl:apply-templates select="myel"/>    </xsl:template>    <xsl:template match="myel">        <xsl:for-each select="@*">            <xsl:value-of select="."/>            <xsl:value-of select="','"/>        </xsl:for-each>        <xsl:text>&#10;</xsl:text>    </xsl:template> </xsl:stylesheet>


If you just want the name attributes of any element, here is a quick but incomplete solution.

(Your example text is in the file example)

grep "name" example | cut -d"\"" -f2,2 | xargs -I{} echo "{},"