Bookmark and Share Share

Formatting Results as CSV

The format for mashup results is typically XML. You can have mashups format the result as CSV (comma-separated values), using the FormatAsCSV macro. This macro is installed with the EMML Reference Runtime Engine and is available for use in any mashup script.

<macro:FormatAsCSV>

Use the following attributes to define the input parameters for this macro and receive the results:

Attribute Required Description

xmldoc

yes

The name of the document-type variable containing the results to format as a CSV string.

For this macro to work correctly, the structure of this input document:

  • Must contain at least one node that repeats.

    Note: this macro finds the first set of repeating nodes in the input document, in a depth-first search, and converts only that set of nodes.

  • Ideally, repeating nodes should contain a single level of children with simple data that is converted to the CSV value.

    If a child of a repeating node also has descendants, then the CSV value for that child appends the values from all its descendants to the value of that child.

  • The element names of all children nodes in the first 'record' (first node in the repeating set) are returned as field headers in the CSV output.

outputvariable

yes

The name of the variable to receive the results of this macro. This variable is a string datatype to receive the CSV-formatted results.

Although this is not required, it is quite typical for this variable to be the output parameter for the mashup that is calling this macro.

<macro:FormatAsCSV> Example

<mashup name="formatCSVBasic" 
    xmlns="http://www.openemml.org/2009-04-15/EMMLSchema" 
    xsi:schemaLocation="http://www.openemml.org/2009-04-15/EMMLSchema 
      ../schema/EMMLSpec.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:macro="http://www.openemml.org/2009-04-15/EMMLMacro"> 
... 
  <variables> 
    <variable name="cvsString" type="string"/> 
    <variable name="sampleXML" type="document"> 
      <sample> 
        <item> 
          <breed>St. Bernard</breed> 
          <category>working dog</category> 
        </item> 
        <item> 
          <breed>Poodle, Standard</breed> 
          <category>working dog</category> 
        </item> 
        ... 
      </sample> 
    </variable> 
  </variable> 
  <macro:FormatAsCSV xmldoc="$sampleXML" outputvariable="$csvString"/> 
...