Creating a Map
This page shows how to build a new map for Middleware4j from nothing, using a small worked example. Read Overview Middleware4j first if you have not already: it explains the three parts of a map.
INPUT CONNECTOR --XML--> MAP (XSLT) --XML--> OUTPUT CONNECTOR(S) native to XML XML to XML XML to native
Building a map always follows the same five steps:
- Choose the input connector for the file you receive.
- Run a sample file through it and look at the XML it produces.
- Look up the XML layout that your output connector expects.
- Write a stylesheet that turns the first into the second.
- Add the stylesheet to the map and test it.
The example
A supplier system writes purchase orders as a CSV file. Two things are needed from each file: an XML document for one system, and a fixed column text file for an older one.
orders.csv
PO10001,43170672,"Stem Bolt, Self Sealing",120,EA,31/08/2026 PO10002,43170673,Reverse Ratchet,40,CS,15/09/2026
One input format and two outputs means one map with a CSV input connector, an XML output connector and an ASCII output connector.
Step 1: add the map with its input connector
Create the folders first. Middleware4j checks that every input and output folder exists when it starts and reports any that are missing.
interface/input/Orders interface/output/OrdersXML interface/output/OrdersFixed
Then add a map to xml/config/config.xml, inside the <maps> element. To begin with it has the input connector and one plain XML output, with no stylesheet. Every setting is explained on the Maps page.
<map>
<id>Orders</id>
<enabled>true</enabled>
<description>Purchase orders CSV to XML and fixed width</description>
<enableEmailNotifications>true</enableEmailNotifications>
<connectors>
<input>
<id>in1</id>
<description>Read orders CSV</description>
<type>CSV</type>
<url>
<path>./interface/input/Orders</path>
<mask/>
<prefix/>
<pollingInterval>1000</pollingInterval>
</url>
<csv>
<optionDelimeter>^</optionDelimeter>
<csvOptions>separator=,^quote="</csvOptions>
</csv>
</input>
<output>
<id>out1</id>
<enabled>true</enabled>
<description>Write orders XML</description>
<type>XML</type>
<url>
<path>./interface/output/OrdersXML</path>
<prefix/>
<fileExtension/>
<use83GUID>false</use83GUID>
<retainOriginalFilename>false</retainOriginalFilename>
</url>
</output>
</connectors>
</map>
Step 2: see what the input connector produces
Start Middleware4j in desktop mode, press Start, and copy orders.csv into interface/input/Orders. Within a second the file disappears from the input folder.
Now look in interface/log. There is a file whose name contains _XSLT_INPUT_:
20261109203242991_XSLT_INPUT_Orders_in1_CSV_orders.csv.xml
This is the XML the input connector made from the CSV file, and it is exactly what your stylesheet will receive. Laid out for reading, it looks like this:
<data cols="6" filename="orders.csv" rows="2" type="CSV">
<row id="1">
<col id="1">PO10001</col>
<col id="2">43170672</col>
<col id="3">Stem Bolt, Self Sealing</col>
<col id="4">120</col>
<col id="5">EA</col>
<col id="6">31/08/2026</col>
</row>
<row id="2">
<col id="1">PO10002</col>
<col id="2">43170673</col>
<col id="3">Reverse Ratchet</col>
<col id="4">40</col>
<col id="5">CS</col>
<col id="6">15/09/2026</col>
</row>
</data>
Because the map has no stylesheet yet, the same XML has also been written to interface/output/OrdersXML.
This is the quickest way to learn what any input connector gives you: run a real file through it and read the result. The layouts are also listed on the Connectors page.
Step 3: find the layout the output connector expects
Each output connector reads XML in one particular layout. They are all listed in What each output connector expects. For this example:
- The XML output connector writes whatever XML it is given, so the stylesheet can produce any layout the receiving system wants.
- The ASCII output connector expects a
<data>element withrowsandcolsattributes, containing numbered<row>and<col>elements. It places each column at the positions given by the<pattern>setting of the connector.
Step 4: write the stylesheets
The two outputs need different XML, so each gets its own stylesheet. Save both in the xslt folder.
Orders_to_XML.xsl builds the XML document. The due date is converted from 31/08/2026 to ISO format with one of the XSLT Extension Functions.
<?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_XSLT_Ext="http://com.commander4j.Transformation" exclude-result-prefixes="xs c4j_XSLT_Ext" version="2.0"> <xsl:output encoding="UTF-8" indent="yes" method="xml"/> <xsl:template match="/data"> <orders> <xsl:apply-templates select="row"/> </orders> </xsl:template> <xsl:template match="row"> <order> <number><xsl:value-of select="col[@id='1']"/></number> <material><xsl:value-of select="col[@id='2']"/></material> <description><xsl:value-of select="col[@id='3']"/></description> <quantity><xsl:value-of select="col[@id='4']"/></quantity> <uom><xsl:value-of select="col[@id='5']"/></uom> <dueDate><xsl:value-of select="c4j_XSLT_Ext:formatDate(col[@id='6'], 'dd/MM/yyyy', 'yyyy-MM-dd''T''HH:mm:ss')"/></dueDate> </order> </xsl:template> </xsl:stylesheet>
Orders_to_Fixed.xsl builds the rows and columns for the fixed column file. It keeps four of the six columns and pads the quantity with leading zeros.
<?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_XSLT_Ext="http://com.commander4j.Transformation"
exclude-result-prefixes="xs c4j_XSLT_Ext" version="2.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:template match="/data">
<data type="ASCII" cols="4" rows="{count(row)}">
<xsl:for-each select="row">
<row id="{position()}">
<col id="1"><xsl:value-of select="col[@id='1']"/></col>
<col id="2"><xsl:value-of select="col[@id='2']"/></col>
<col id="3"><xsl:value-of select="c4j_XSLT_Ext:padStringLeft(col[@id='4'], 8, '0')"/></col>
<col id="4"><xsl:value-of select="col[@id='5']"/></col>
</row>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>
Notice that this stylesheet does not write a text file. It writes XML rows and columns, and the ASCII output connector turns them into text.
Step 5: add the stylesheets and the second output
Replace the <output> section of the map with the two outputs below. Each names its stylesheet in an <xsl> element, and the ASCII output has a <pattern> giving the start and end position of each column.
<output>
<id>out1</id>
<enabled>true</enabled>
<description>Write orders XML</description>
<type>XML</type>
<url>
<path>./interface/output/OrdersXML</path>
<prefix/>
<fileExtension/>
<use83GUID>false</use83GUID>
<retainOriginalFilename>false</retainOriginalFilename>
</url>
<xsl>
<XSLT>Orders_to_XML.xsl</XSLT>
</xsl>
</output>
<output>
<id>out2</id>
<enabled>true</enabled>
<description>Write fixed width file</description>
<type>ASCII</type>
<url>
<path>./interface/output/OrdersFixed</path>
<prefix/>
<fileExtension/>
<use83GUID>false</use83GUID>
<retainOriginalFilename>true</retainOriginalFilename>
</url>
<xsl>
<XSLT>Orders_to_Fixed.xsl</XSLT>
</xsl>
<ascii>
<pattern>1-10,12-19,21-28,30-32</pattern>
</ascii>
</output>
Restart Middleware4j so that it reads the changed config.xml, and copy orders.csv into the input folder again.
interface/output/OrdersXML/Orders_out1_orders.csv.xml
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order>
<number>PO10001</number>
<material>43170672</material>
<description>Stem Bolt, Self Sealing</description>
<quantity>120</quantity>
<uom>EA</uom>
<dueDate>2026-08-31T00:00:00</dueDate>
</order>
<order>
<number>PO10002</number>
<material>43170673</material>
<description>Reverse Ratchet</description>
<quantity>40</quantity>
<uom>CS</uom>
<dueDate>2026-09-15T00:00:00</dueDate>
</order>
</orders>
interface/output/OrdersFixed/orders.csv.txt
PO10001 43170672 00000120 EA PO10002 43170673 00000040 CS
The two output files are named differently because the first output has retainOriginalFilename set to false and the second has it set to true. See Output file names.
Seeing what happened
Middleware4j keeps a copy of every stage in interface/log. For the example above the folder holds six files for one input file:
| File name contains | What it is |
|---|---|
_INPUT_BACKUP_Orders_in1_CSV_ |
An untouched copy of the file that arrived. |
_XSLT_INPUT_Orders_in1_CSV_ |
The XML made by the input connector. |
_XSLT_INPUT_Orders_out1_XML_ |
The XML given to the stylesheet of output out1.
|
_XSLT_OUTPUT_Orders_out1_XML_ |
The XML that stylesheet produced, which was passed to the XML output connector. |
_XSLT_INPUT_Orders_out2_ASCII_ |
The XML given to the stylesheet of output out2.
|
_XSLT_OUTPUT_Orders_out2_ASCII_ |
The XML that stylesheet produced, which was passed to the ASCII output connector. |
Each name starts with the date and time to the millisecond, followed by the map id, the connector id and the connector type, so the files for one map sort together in order.
When an output is not what you expected, open the _XSLT_OUTPUT_ file for that output:
- If the XML in it is wrong, the fault is in the stylesheet.
- If the XML is right but the final file is wrong, compare it with the layout the output connector expects.
You can also develop a stylesheet outside Middleware4j. Copy an _XSLT_INPUT_ file from the log folder and run your stylesheet against it in an XSLT editor until the result is right. The XSLT Extension Functions are only available inside Middleware4j, so leave those calls until last.
Files in interface/log are deleted automatically after the number of days set by logArchiveRetentionDays.
Where to put the stylesheet
A stylesheet can be attached to the input connector, to an output connector, or both.
| Attached to | Runs | Use it when |
|---|---|---|
| The input connector | Once for each file. Every output receives its result. | All outputs need the same XML, or the map has only one output. Most of the sample maps work this way. |
| An output connector | Once for that output only. | Different outputs need different XML, as in the example on this page. |
| Both | The input stylesheet runs first and each output stylesheet then starts from its result. | The input stylesheet tidies the data into a common layout and each output stylesheet makes a small final change. |
Sending a file to different places depending on its content
An output can be given a <condition>. The output is only written when the condition is true, so one map can route a file to different folders according to a value inside it. The sample map Map13 sends a production declaration to one folder when its storage bin is DESPATCH and to another when it is not. See Conditions.
Using reference data
Values that differ from site to site, such as a plant code, are better kept out of the stylesheet. The sample stylesheets read them from small XML files in the xslt folder:
| File | Purpose |
|---|---|
configData.xml |
Single values for the site, such as the host reference, plant and warehouse. |
referenceData.xml |
Translation tables, for example from an ERP plant and storage location to a Commander4j location. |
UOMData.xml |
Unit of measure details for each material. |
A stylesheet reads a value with a small function of its own. This one is taken from the sample Map04_CSV_to_XML.xsl:
<xsl:variable name="HOSTREF"><xsl:value-of select="c4j:getConfigItem('config','HostRef')"/></xsl:variable>
<xsl:function name="c4j:getConfigItem">
<xsl:param name="type"/>
<xsl:param name="string1"/>
<xsl:variable name="item_info" select="document('configData.xml')/lookup"/>
<xsl:value-of select="$item_info/item[@type=$type][@id=$string1]/value"/>
</xsl:function>
The stylesheet must declare the c4j prefix for this to work: xmlns:c4j="http://www.commander4j.com".
Checklist when a map does nothing
| Check | Why |
|---|---|
The map and each output have <enabled>true</enabled> |
A map or output without it is ignored. The value must be the word true.
|
| The file extension matches the mask | With an empty <mask/> each connector type only picks up its own extensions, for example csv for a CSV input. See Which files are picked up.
|
| The file is not empty | Empty files are left in the input folder. |
| Middleware4j was restarted | config.xml is only read at start up.
|
| The stylesheet output has a single root element | The result is read back as an XML document before it is passed to the output connector. |
| The stylesheet output matches the layout for the output connector | A CSV output, for example, writes an empty file if the rows have no cols attribute. See Connectors.
|
The logs folder |
logs/c4jMiddleware.log records every step and any error message from the stylesheet.
|
See also: Maps, Connectors, XSLT Extension Functions, Middleware4j Example Configuration