Skip to content

Using XML Library

This article will give an overview of how the XML and XSLT tasks can be used within a workflow to process and transform data.  XML is used as the input and output of virtually all Zynk tasks, with various different object models being used. XSLT (and the Auto Mapper task) can be used to convert data to the required format.

Input XML

All of the examples are based around the following StockTransaction XML, which should be saved out to your working directory as input.xml

<?xml version="1.0"?>
<Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <StockTransactions>
        <StockTransaction>
            <UniqueId>1</UniqueId>
            <StockTransactionType>AdjustmentIn</StockTransactionType>
            <StockCode>BOARD001</StockCode>
            <StockTransactionDate>2006-12-31T00:00:00</StockTransactionDate>
            <Reference>STK TAKE</Reference>
            <Details>Whiteboard - Drywipe (900 x 1200)</Details>
            <Qty>2</Qty>
            <CostPrice>15</CostPrice>
            <SalesPrice>0</SalesPrice>
            <StockTransactionNumber>1</StockTransactionNumber>
        </StockTransaction>
        <StockTransaction>
            <UniqueId>2</UniqueId>
            <StockTransactionType>AdjustmentIn</StockTransactionType>
            <StockCode>BOARD002</StockCode>
            <StockTransactionDate>2006-12-31T00:00:00</StockTransactionDate>
            <Reference>STK TAKE</Reference>
            <Details>Whiteboard - Drywipe (1000 x 1500)</Details>
            <Qty>2</Qty>
            <CostPrice>17</CostPrice>
            <SalesPrice>0</SalesPrice>
            <StockTransactionNumber>2</StockTransactionNumber>
        </StockTransaction>
    </StockTransactions>
</Company>

XML Split Example

XML Split can be used to batch large data sets into smaller chunks for processing, for example if you are uploading a large number of products to a website you might want to split the file and send as multiple requests.

As an example we will split the above input XML file using the following settings:

Input File

Path to input.xml

Output Folder

XML

Record Limit

1

XPath Query

/Company/StockTransactions/StockTransaction

The result of the task will be two files created in the XML folder in the working directory.  In the sample workflow there is a File Repeater task that will write out the names of the generated files using a Log Info Message task.

XML Merge Example

XML Merge can be used to create a single XML file from a number of smaller documents, for example if your third party systems exports orders to individual files you can create a single import file for easier processing.

As an example we will merge back the two tasks from above using the following settings:

Input Files

(Output from previous task)

Output File

input2.xml

In the sample workflow there is a List Files task above the XML Merge task, this will provide the file listing from the XML folder to the Input Files setting.  The result of the task will be a single file created called input2.xml in the working directory.

XML Repeater Example

XML Repeater can be used if you need to run part of your workflow based on the contents of an XML document, for example if you need to update a database based on the values in the sample file you could build up and execute a SQL statement based on the contents of the file.

As an example we will write out the StockCode values from the above output file with the following settings:

Input File

input2.xml

XPath Query

/Company/StockTransactions/StockTransaction

In the sample workflow we have a Log Info Message task that will write out the StockCode for each of the StockTransactions.

XML to CSV Example

XML to CSV can be used to create simple or complex mappings from XML to CSV, including support for flatting hierarchical data e.g. orders with order items in our XML format to a flat CSV format, for example to create a report to open in Excel.

As an example we will convert the output file from above into a simple CSV format with the following settings:

Input File

input2.xml

Mapping Fields

From To Format
StockCode Code string
StockTransactionType type string
Qty quantity decimal
CostPrice cost decimal (with format c)

Output File

output.csv

XPath

/Company/StockTransactions/StockTransaction

In the sample workflow we have a Log Info Message task that will write out the contents of the CSV file generated by the workflow.

XSLT Transformation Example

XSLT Transformation is used in most integrations to convert between different XML formats, and the Auto Mapper tasks provides a collection of pre-canned mappings that can be used to quickly build workflows.

Save the below to transform.xslt

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <Rows>
            <xsl:for-each select="Company/StockTransactions/StockTransaction">
                <xsl:call-template name="ProcessRow" />
            </xsl:for-each>
        </Rows>
    </xsl:template>

    <xsl:template name="ProcessRow">
        <Row>
            <Code><xsl:value-of select="StockCode" /></Code>
            <Type><xsl:value-of select="StockTransactionType" /></Type>
            <Quantity><xsl:value-of select="Qty" /></Quantity>
            <Cost><xsl:value-of select="CostPrice" /></Cost>
        </Row>
    </xsl:template>
</xsl:stylesheet>

As an example we will convert the output file from the above into a simple XML format with the following settings:

Input File

input2.xml

Output File

output.xml

XSLT File

transform.xslt

In the sample workflow we have a Log Info message task that will write out the contents of the XML file generated by the workflow.