Converting XML to CSV using Scriptella, how to get attribute values?

I found an example of converting XML to CSV. In the used example, 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, the 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 to convert an xml file with 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 attribute values ​​in XML?

+4
source share
1 answer

You need to first correctly describe the drivers for all your connections. You cannot parse XML with Scriptella unless you use the xpath driver. More information: http://scriptella.org/reference/drivers.html

: - java- , 2 , - xml β†’ xpath - csv β†’ csv - csv, .

xml data.xml, csv data.csv, scriptella etl script:

<!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd">
<etl>
  <connection id="in" driver="xpath" url="data.xml" />
  <connection id="out" driver="csv" url="data.csv">
    quote=
    separator=;
  </connection>
  <script connection-id="out">
    TITLE,ARTIST,COUNTRY,COMPANY,PRICE,YEAR
  </script>
  <query connection-id="in">
    /CATALOG/CD
    <script connection-id="out">
      $TITLE,$ARTIST,$COUNTRY,$COMPANY,$PRICE,$YEAR
    </script>
  </query>
</etl>

, XML-. $TITLE, $TITLE $TITLE, <TITLE> XML.

rownum ETL.

0

Source: https://habr.com/ru/post/1629871/


All Articles