Skip to content

Instantly share code, notes, and snippets.

@blaggacao
Created November 30, 2018 00:33
Show Gist options
  • Save blaggacao/4a677a7869d87e4084c83c1f0fb05c40 to your computer and use it in GitHub Desktop.
Save blaggacao/4a677a7869d87e4084c83c1f0fb05c40 to your computer and use it in GitHub Desktop.
Odoo XML to CSV the easy way
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="delimiter" select="','" />
<!-- deinfe an array containing the fields we are interested in -->
<xsl:variable name="fieldArray">
<field>name</field>
<field>code</field>
<field>financial_report_id</field>
<field>parent_id</field>
<field>sequence</field>
<field>level</field>
<field>formulas</field>
<field>domain</field>
<field>groupby</field>
<field>special_date_changer</field>
</xsl:variable>
<xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*"/>
<!-- output header row -->
<xsl:template match="/">
<!-- the id column -->
<xsl:text>id</xsl:text>
<xsl:for-each select="$fields">
<xsl:value-of select="$delimiter"/>
<xsl:value-of select="." />
</xsl:for-each>
<!-- output newline -->
<xsl:text>&#xa;</xsl:text>
<xsl:apply-templates select="*/record"/>
</xsl:template>
<xsl:template match="record">
<xsl:variable name="currNode" select="."/>
<!-- the id column -->
<xsl:value-of select="$currNode/@id"/>
<!-- output the data row -->
<!-- loop over the field names and find the value of each one in the xml -->
<xsl:for-each select="$fields">
<xsl:value-of select="$delimiter"/>
<xsl:text>"</xsl:text>
<xsl:if test="$currNode/field[@name = current()]/@ref">
<xsl:value-of select="$currNode/field[@name = current()]/@ref"/>
</xsl:if>
<xsl:if test="$currNode/field[@name = current()]/@eval">
<xsl:value-of select="$currNode/field[@name = current()]/@eval"/>
</xsl:if>
<xsl:if test="$currNode/field[@name = current()] != ''">
<xsl:value-of select="$currNode/field[@name = current()]"/>
</xsl:if>
<xsl:text>"</xsl:text>
</xsl:for-each>
<!-- output newline -->
<xsl:text>&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment