Now we can use the transformation above when we build a workflow, which converts the XML file into a CSV file.
The workflow has these components:
Each of the elements are described below from the left to the right:
Here we setup a workflow to monitor a folder for new, incoming XML files:
To make it easier to define and maintain the workflow a sample file is referenced. This is a sample file, that has been created by the transformation template, which was created in the previous section.
Now we execute the transformation template we defined in the previous section:
This change the payload from the original input Invoice xml file into the simple XML file shown in the transformation section above, which contains only a root node and multiple line nodes inside.
Now we setup multiple variable to prepare for the processing:
Here these variables are defined:
NbrOfLines
The value is the number of line nodes in the transformed XML file. You can see the simple XML file referenced in the transformation section above.
Index
We intend to select each line node one by one and for that we need this index variable, which will have the initial value of 1 and is to be increased with 1 for each repetition.
CSV_data
This is the output variable into which we will build the CSV data. This starts with an empty string.
The choice and join element has a dummy condition, which is always true:
The purpose of the choice and join is simply to execute all the repeats, which will collect output data into the CSV_data variable and then continue with the join branch to output the result.
The repeat is executed as many times as the number of line nodes in the transformed XML file as seen below. The option, Keep variable values and payload between repeats is activated and that is very important to remember (if not activated the value of the Index and CSV_data variables cannot be updated for each iteration).
It is also important to use the number() function to convert the variable into a number as workflow variables are handled as strings.
Inside the repeat we update the variables, CSV_data and Index as below:
CSV_data is defined as the current value of CSV_data concatenated with a string which contains a line break (This was added a two single quotes and then pressing Enter inside to add the line break). It is also concatenated with the value (contents) of the current line node. The current line node is selected with [number($Index)], so we use the Index variable to select the current line node.
In this manner the CSV_data will contain a line break and then contents of all line nodes separated by another line break. Later we need to remove the initial line break.
After the update of the CSV_data we increase the value of the Index variable with 1, so that it increase with one for each repetition in the repeat.
Notice the number() function is used for both variable updates.
Now we consider the join branch, which is executed after the repeats above. Here we remove the first character (the initial line break) from the CSV_data with this expression:
The substring function simply extracts the contents of the CSV_data variable - from position 2.
The last component outputs the CSV file to the file system. That is done like below: