Writing an XML Document

    Before discussing how to create the code used to write an XML document, we need to review the Lua Object Model.

     

    Each element in the Expressor Datascript table that is the Lua Object Model consists of an Expressor Datascript table with the following elements.

    • tag: a string indexed element that contains the element (tag) name
    • attr: an Expressor Datascript table that contains element attributes, if present
      • This table contains both string and numerically indexed values
    • One or more numerically indexed elements
      • If the Lua Object Model element contains CDATA, index 1 refers to this value
      • If the Lua Object Model element contains a child element, index 1 refers to a nested Lua Object Model table

     

    Let's see how this is applied to an actual XML document.

    xml_document_7.png

    At the top of the Lua Object Model would be an Expressor Datascript table corresponding to the Root element.  This table's element referenced through the string index value tag, would hold the value Root, the name of the element.  This table's element referenced through the string index value attr, would be an empty table, and the element referenced through the numeric index value 1 would be another Expressor Datascript table corresponding to the Customers element.

     

    The table corresponding to the Customers element would have the tag value Customers; the attr index would reference an empty table; and there would be multiple numerically index elements referencing each of the Customer elements.

     

    Each table representing a Customer element would require attribute entries.  In this case, the table referenced by the attr index would contain one numerically indexed entry and one string indexed entry for each attribute (note that in this example, the Customer element contains only one attribute).  The numerically indexed entry's value would be the attribute name, and the string indexed value would be the attribute's value.  So for each Customer element, the table referenced through attr would contain the following entries.

    attr = {[1] = "CustomerID",CustomerID="XYZ"}

    And for the second Customer

    attr = {[1] = "CustomerID",CustomerID="ABC"}

     

    As an example, consider the following input data, which you want to emit as an XML document

    xml_document_9.png

    Each record represents a customer and all of the data, with the exception of the CustomerID, should be separate elements.  The CustomerID should be an attribute of the Customer element.  To get started, you need to create the elements in the Lua Object Model corresponding to the Root element and the parent element of the Customer elements.  Your coding would look something like the following.

    xml_document_10.png

    Then as you process each record, you need to create a nested table within the AllCustomers table to correspond to each customer and within this child table a collection of child tables corresponding to each of the CDATA elements.  The code also needs to differentiate between element and attribute data.

     

    What's the appropriate operator in which to perform the processing?  Well, since you want to process all the incoming records into a single document, you would use an Aggregate operator so that details from every record can be collected into a single table that represents the Lua Object Model.  The following screen shot is an example. 

    xml_document_8.png

    The aggregate function handles the details of creating the Lua Object Model representation.  Note how the attribute and element data are distinguished and processed differently in lines 16-26. 

     

    But in order to emit the data as an XML document, you need to convert the Lua Object Model into a string.  Code capable of performing this processing is not included with Expressor, so it must be developed as an Expressor Datascript Module.  The attached file contains example code, which should be considered as only a starting point for your own efforts and not a production quality implementation, illustrates how the Lua Object Model can be decomposed into an XML document.  Note how in the above screen shot, lines 2 and 31 refer to this datascript module and its public function lomToXml.  The return from this function is a string containing the entire contents of the XML document.