ASCII
The ASCII connector reads and writes text files in which each field sits at a fixed column position. It can be used for input and output.
The key setting is the <pattern>, a list of start and end positions, one pair for each field. Positions are counted from 1, so 1-4,18-21 means two fields: characters 1 to 4 and characters 18 to 21.
See Example Configuration for a full walk through of the ASCII input example below.
ASCII Input Map Configuration
config.xml
<map>
<id>Map02</id>
<enabled>true</enabled>
<description>ASCII File Fixed Column to XML</description>
<enableEmailNotifications>true</enableEmailNotifications>
<connectors>
<input>
<id>in1</id>
<description>Read ASCII Flat File</description>
<type>ASCII</type>
<url>
<path>./interface/input/Map02 ASCII to XML</path>
<mask/>
<prefix/>
<pollingInterval>1000</pollingInterval>
</url>
<xsl>
<XSLTPath/>
<XSLT>Map02_ASCII_to_XML.xsl</XSLT>
</xsl>
<ascii>
<pattern>1-4,18-21</pattern>
</ascii>
</input>
<output>
<id>out1</id>
<enabled>true</enabled>
<description>Write XML</description>
<type>XML</type>
<url>
<path>./interface/output/Map02 ASCII to XML</path>
<prefix/>
<fileExtension/>
<use83GUID>false</use83GUID>
</url>
</output>
</connectors>
</map>
The setting which is particular to this connector is <pattern>1-4,18-21</pattern> inside the <ascii> element. This determines the columns within the input file which are read into memory and optionally processed by the XSLT Style Sheet.
Each line of the file becomes a <row> and each range in the pattern becomes a <col>, numbered in the order the ranges are listed. The values keep any spaces they contain. Every line of the file must be at least as long as the last position in the pattern, otherwise the file is rejected and renamed with .error on the end.
The above scenario is documented in some detail within the Example Configuration page.
ASCII Map Output Configuration
<map>
<id>Map09</id>
<enabled>true</enabled>
<description>XML to ASCII Flat File</description>
<enableEmailNotifications>true</enableEmailNotifications>
<connectors>
<input>
<id>in1</id>
<description>Read XML File</description>
<type>XML</type>
<url>
<path>./interface/input/Map09 XML to ASCII</path>
<mask/>
<prefix/>
<pollingInterval>1000</pollingInterval>
</url>
<xsl>
<XSLTPath/>
<XSLT>Map09_XML_to_ASCII.xsl</XSLT>
</xsl>
</input>
<output>
<id>out1</id>
<enabled>true</enabled>
<description>Write ASCII File</description>
<type>ASCII</type>
<url>
<path>./interface/output/Map09 XML to ASCII</path>
<prefix/>
<fileExtension/>
<use83GUID>false</use83GUID>
</url>
<ascii>
<pattern>1-10,12-21,23-53</pattern>
</ascii>
</output>
</connectors>
</map>
The output connector is very similar to configure and in this example we use a XSL file called to create a Flat ASCII file from a standard Commander4j XML message. The XSL source in this example looks like this. In this scenario we want to extract and export the order, material and description from the xml and write to a flat ascii file. We are going to write the data to 3 column ranges defined in <pattern>1-10,12-21,23-53</pattern>
Sample XSL File
Map09_XML_to_ASCII.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:c4j="http://www.commander4j.com"
xmlns:c4j_XSLT_Ext="http://com.commander4j.Transformation"
exclude-result-prefixes="xs c4j c4j_XSLT_Ext" version="2.0">
<xsl:output encoding="UTF-8" indent='yes' method="xml" />
<xsl:strip-space elements="*"/>
<xsl:template match="text() | @*"/>
<xsl:variable name="sscc" select="/message/messageData[1]/productionDeclaration[1]/SSCC"/>
<xsl:template match="message/messageData/productionDeclaration">
<data cols="15" rows="1" type="CSV">
<row id="1">
<col id="1"><xsl:value-of select="processOrder"/></col>
<col id="2"><xsl:value-of select="material"/></col>
<col id="3"><xsl:value-of select="c4j_XSLT_Ext:removeCommas(description)"/></col>
<col id="4"><xsl:value-of select="floor(number(productionQuantity))"/></col>
<col id="5"><xsl:value-of select='name' /></col>
<col id="6"><xsl:value-of select="c4j_XSLT_Ext:formatDate(productionDate, 'yyyy-MM-dd''T''HH:mm', 'dd-MMM-yyyy')"/></col>
<col id="7"><xsl:value-of select="batch"/></col>
<col id="8"><xsl:value-of select="SSCC"/></col>
<col id="9"><xsl:value-of select="c4j_XSLT_Ext:formatDate(expiryDate, 'yyyy-MM-dd''T''HH:mm', 'dd-MMM-yyyy')" /></col>
<col id="10"><xsl:value-of select="warehouse"/></col>
<col id="11"><xsl:value-of select="storageLocation"/></col>
<col id="12"><xsl:value-of select="c4j_XSLT_Ext:formatDate(expiryDate, 'yyyy-MM-dd''T''HH:mm', 'dd-MMM-yyyy')" /></col>
<col id="13"><xsl:value-of select="c4j_XSLT_Ext:formatDate(productionDate, 'yyyy-MM-dd''T''HH:mm', 'dd-MMM-yyyy')"/></col>
<col id="14"><xsl:value-of select="old_code"/></col>
<col id="15"><xsl:value-of select="c4j_XSLT_Ext:subString($sscc, 8, 9)"/></col>
</row>
</data>
</xsl:template>
</xsl:stylesheet>
The above style sheet creates a intermediate XML file which is then passed to the ASCII connector to write the flat file. The ASCII output connector reads the rows and cols attributes of the <data> element to find out how much data there is, so both must be present, and it finds each value by the id attributes of its <row> and <col>. Column 1 is written at the first range of the pattern, column 2 at the second, and so on. Columns beyond the last range in the pattern are ignored, which is why only three of the fifteen columns in this example reach the output file. A value longer than its range is cut off. You will notice that the XSL includes some functions. The description for example uses a function to remove commas. You can find more information regarding these functions in XSLT Extension Functions
Lets work through the sample files to understand what is happening here.
Sample XML Input File (Commander4j Production Declaration message)
XML to ASCII Flat File 1.xml
<?xml version="1.0" encoding="UTF-8"?>
<message>
<hostRef>service</hostRef>
<messageRef>12584314</messageRef>
<interfaceType>Production Declaration</interfaceType>
<messageInformation>SSCC=000000000000000123</messageInformation>
<interfaceDirection>Output</interfaceDirection>
<messageDate>2016-08-01T14:58:16</messageDate>
<messageData>
<productionDeclaration>
<SSCC>000000000000000123</SSCC>
<processOrder>20725187</processOrder>
<recipe>100000000028949253</recipe>
<requiredResource>P2VK1112</requiredResource>
<material>43170672</material>
<description>Self Sealing Stem Bolt</description>
<old_code/>
<ean>07613032213848</ean>
<variant>00</variant>
<status>Unrestricted</status>
<batch>6214093310</batch>
<batchStatus>Restricted</batchStatus>
<expiry_Mode>BATCH</expiry_Mode>
<expiryDate>2018-08-31T00:00:00</expiryDate>
<location>EARTH</location>
<name>Earth</name>
<gln>5000161800292</gln>
<plant>0933</plant>
<warehouse>734</warehouse>
<storageLocation>0001</storageLocation>
<storageSection/>
<storageBin>DESPATCH</storageBin>
<storageType>PRO</storageType>
<productionQuantity>5616.000</productionQuantity>
<productionUOM>EA</productionUOM>
<confirmed>Y</confirmed>
<productionDate>2016-08-01T14:58:03</productionDate>
</productionDeclaration>
</messageData>
</message>
Intermediate XML (result of appling XSL to input XML message)
<?xml version="1.0" encoding="UTF-8"?>
<data cols="15" rows="1" type="CSV">
<row id="1">
<col id="1">20725187</col>
<col id="2">43170672</col>
<col id="3">Self Sealing Stem Bolt</col>
<col id="4">5616</col>
<col id="5">Earth</col>
<col id="6">01-Aug-2016</col>
<col id="7">6214093310</col>
<col id="8">000000000000000123</col>
<col id="9">31-Aug-2018</col>
<col id="10">734</col>
<col id="11">0001</col>
<col id="12">31-Aug-2018</col>
<col id="13">01-Aug-2016</col>
<col id="14"/>
<col id="15">000000012</col>
</row>
</data>
When the XSL executed against the input XML the above intermediate file is created. This is then passed to the output connector which matches the data found in the xml above to the column definitions.
Output ASCII
20725187 43170672 Self Sealing Stem Bolt
This is the output from the transformation. The intermediate files can be found in the interface/log directory. For more information see the Example Configuration
See also: Connectors, Maps, Creating a Map