Create a VOTable version of a CSV table. The transform must work for all possible CSV tables.
Our data is a table that contains information about projects. It has three fields: id, project name and a version. We want to create an VOTable version of the CSV table.
Our CSV table looks like:
# Example table data. id,project,version apple,Appleseed,version-1 orange,OrangeBlossom,version-2
We will store this in a file called projects.csv.
Our velocity template looks like:
<!-- Example use of table data with Velocity -->
<!-- Converts any CSV table to a VOTable -->
<VOTABLE version="1.3">
<RESOURCE name="$table.name">
<TABLE name="results">
<DESCRIPTION>$table.description</DESCRIPTION>
#foreach ($field in $table.fields)
<FIELD name="$field" datatype="char" />
#end
<DATA>
<TABLEDATA>
#foreach ($record in $table.records)
<TR>
#foreach ($field in $table.fields)
<TD>$record[$field]</TD>
#end
</TR>
#end
</TABLEDATA>
</DATA>
</TABLE>
</RESOURCE>
</VOTABLE>
We will store this in a file called example-votable.vm.
In the template we expect our table to be in a context called "table" (which is established on the command line - see below). A VOTable has two parts. In the first part is metadata which describes the table and each field in the table. To create this metadata we use $table.name which contains the name of the CSV data file and $table.description which contains all information contained in comments in the CSV data file. We then iterate over the list of fields in the CSV table ($table.fields) and describe each field. The next part of a VOTable is the data itself. To generate this information we loop over the records in the table ($table.record) and use the list of fields ($table.fields) to determine the value for each field in the record.
Running igpp.docgen with the command:
Instructs igpp.docgen to parse the file "projects.csv" and place the results in a context named "table". Since the extension on "projects.csv" is ".csv" igpp.docgen will parse the file as a set of comma separated values, creating a record for each non-commented line in the file.
Running this command will generate an XML document that looks like:
<?xml version="1.0" encoding="UTF-8"?>
<VOTABLE version="1.3">
<RESOURCE name="projects.csv">
<TABLE name="results">
<DESCRIPTION>Example table data.</DESCRIPTION>
<FIELD datatype="char" name="id"/>
<FIELD datatype="char" name="project"/>
<FIELD datatype="char" name="version"/>
<DATA>
<TABLEDATA>
<TR>
<TD>apple</TD>
<TD>Appleseed</TD>
<TD>version-1</TD>
</TR>
<TR>
<TD>orange</TD>
<TD>OrangeBlossom</TD>
<TD>version-2</TD>
</TR>
</TABLEDATA>
</DATA>
</TABLE>
</RESOURCE>
</VOTABLE>