I found an example of converting XML to CSV,In the example used, this structure
<!-- Demo input for ETL -->
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
In this file structure, Scriptella code:
<script connection-id="out">Title;Artist;Country;Company;Price;Year</script>
<query connection-id="in">
<!--XPath which all CD elements in a catalog-->
/CATALOG/CD
<!--Outputs all matched elements-->
<script connection-id="out" if="rownum>1">$TITLE;$ARTIST;$COUNTRY;$COMPANY;$PRICE;$YEAR</script>
</script>
How can I convert the XML file that has the following structure
<CATALOG>
<CD title='Empire Burlesque' artist='Bob Dylan' country='USA'/>
.............
<CD title='Empire Burlesque' artist='Bob Dylan' country='USA'/>
</CATALOG>
How do I get to the values of attributes in XML?
You need first to have properly described drivers for all your connections. You cannot parse XML with Scriptella unless you use the xpath driver. More information there: http://scriptella.org/reference/drivers.html
Now for the magic bits: - you could use java libraries as alternate possibilities but since these 2 drivers are supported out of the box, I suggest to go with them - you wish to import xml -> xpath driver is needed - you wish to export csv -> csv driver is needed - text driver can also be used for outputting csv data, but you'd have to handle quoting and separators manually
If your xml data is in file
data.xmland you wish to export it as csv data in filedata.csv, I would suggest using the following scriptella etl script:Please respect the case used inside XML source. You must use
$TITLEand not$titlenor$Title, since<TITLE>is present in your XML source.The rownum test is not needed for such ETL task.