Scenario

Using tables with known structures, create a simple XML document using the contents of table with a known structure.


Our data is a table that contains start and stop times of individual events. It has three fields: start_time, stop_time, event. We want create a table of all events, along with events that occurred during the "start" time followed by the events which ocurred within the "start" and "stop" time span.

Our data table is in CSV format and looks like:

# Example table data.
start_time,stop_time,event
1995-12-07,1997-12-16,Prime Mission
1997-12-16,2000-01-03,Europa Mission
2000-01-03,2003-12-21,Millennium Mission

We will store this in a file called events.csv.

Our velocity template looks like:

<!-- Example use of table data with Velocity -->
<!-- Source table should have at least three columns with the names "start_time", "stop_time", and "event" -->
<!-- it is loaded into the context "table", time to search for is passed on the command-line as "start" and "stop" -->
<Doc>
   <records>${table.records.size()}</records>
   <events>
#foreach ($record in $table.records )
      <event>
         <name>$record.event</name>
         <start_time>$record.start_time</start_time>
         <stop_time>$record.stop_time</stop_time>
      </event>
#end
   </events>
   
   <events>
      <at>$options.start</at>
#foreach ($record in $table.findBetween("start_time", "stop_time", $options.start).records )
      <event>
         <name>$record.event</name>
         <start_time>$record.start_time</start_time>
         <stop_time>$record.stop_time</stop_time>
      </event>
#end
   </events>
   
   <events>
      <span>
         <from>$options.start</from>
         <to>$options.stop</to>
     </span>
#foreach ($record in $table.findSpan("start_time", "stop_time", $options.start, $options.stop).records )
      <event>
         <name>$record.event</name>
         <start_time>$record.start_time</start_time>
         <stop_time>$record.stop_time</stop_time>
      </event>
#end
   </events>
</Doc>

We will store this in a file called example-events.vm.

In the template we expect our table to be in a context called "table" (which is established on the command line - see below). We will output a count of the number of records in the table (${table.record.size()}) and for each record we will output the value for each field marked up with XML tags.

Running igpp.docgen with the command:

$ java -jar igpp.docgen start=1998 stop=2003 table:events.csv example-events.vm

Instructs igpp.docgen to parse the file "events.csv" and place the results in a context named "table". Since the extension on "events.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"?>
<Doc>
   <records>3</records>
   <events>
      <event>
         <name>Prime Mission</name>
         <start_time>1995-12-07</start_time>
         <stop_time>1997-12-16</stop_time>
      </event>
      <event>
         <name>Europa Mission</name>
         <start_time>1997-12-16</start_time>
         <stop_time>2000-01-03</stop_time>
      </event>
      <event>
         <name>Millennium Mission</name>
         <start_time>2000-01-03</start_time>
         <stop_time>2003-12-21</stop_time>
      </event>
   </events>
   
   <events>
      <at>1998</at>
      <event>
         <name>Europa Mission</name>
         <start_time>1997-12-16</start_time>
         <stop_time>2000-01-03</stop_time>
      </event>
   </events>
   
   <events>
      <span>
         <from>1998</from>
         <to>2003</to>
     </span>
      <event>
         <name>Europa Mission</name>
         <start_time>1997-12-16</start_time>
         <stop_time>2000-01-03</stop_time>
      </event>
      <event>
         <name>Millennium Mission</name>
         <start_time>2000-01-03</start_time>
         <stop_time>2003-12-21</stop_time>
      </event>
   </events>
</Doc>