Import Transformation Patterns (XSLT)
This article provides practical, ready-to-use XSLT 1.0 code for the most common data manipulation tasks. Each pattern is presented in a separate section. Within each section, you'll find tabs explaining the Goal, showing the Source XML you're starting with, the XSLT Stylesheet needed to transform it, and the final Resulting Target XML.
You can use these patterns as a starting point and adapt them to your specific needs in the Transformation Editor.
This is the most fundamental pattern. It shows how to convert the standard source XML generated from a CSV file into the required target format.
Goal
Convert a simple CSV structure to the <importedData> format, which is required by the importer.
Source XML (from a CSV)
- <root>
- <row>
- <StudentID>101</StudentID>
- <FirstName>Jane</FirstName>
- <LastName>Doe</LastName>
- </row>
- <row>
- <StudentID>102</StudentID>
- <FirstName>John</FirstName>
- <LastName>Smith</LastName>
- </row>
- </root>
XSLT Stylesheet
- <?xml version="1.0"?>
- <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:date="urn:date" xmlns:math="urn:math" xmlns:str="urn:str" exclude-result-prefixes="date math str">
- <xsl:template match="Root">
- <importedData>
- <xsl:for-each select="row">
- <row>
- <column header="ID">
- <xsl:value-of select="StudentID"/>
- </column>
- <column header="FName">
- <xsl:value-of select="FirstName"/>
- </column>
- <column header="LName">
- <xsl:value-of select="LastName"/>
- </column>
- </row>
- </xsl:for-each>
- </importedData>
- </xsl:template>
- </xsl:transform>
How the Code Works
- <xsl:template match="/root">: This line starts a template that matches the <root> element of your source XML.
- <importedData>: This creates the main wrapper element required for the target format.
- <xsl:for-each select="row">: This is a loop. It finds every <row> element inside <root> and repeats the code within it for each one.
- <column header="...">: Inside the loop, this creates the required <column> element for each piece of data.
- <xsl:value-of select="StudentID"/>: This finds the value of the <StudentID> element from the source XML and places it inside the <column> tag.
Resulting Target XML
- <importedData>
- <row>
- <column header="ID">101</column>
- <column header="FName">Jane</column>
- <column header="LName">Doe</column>
- </row>
- <row>
- <column header="ID">102</column>
- <column header="FName">John</column>
- <column header="LName">Smith</column>
- </row>
- </importedData>
This pattern shows how to concatenate values from two source nodes (FirstName and LastName) into a single FullName column.
Goal
Combine a first and last name into a single "FullName" field.
Source XML (from a CSV)
- <root>
- <row>
- <FirstName>Jane</FirstName>
- <LastName>Doe</LastName>
- </row>
- </root>
XSLT Stylesheet
This snippet would be placed inside the <row> element of your main template (as shown in Pattern 1).
- <column header="FullName">
- <xsl:value-of select="concat(FirstName, ' ', LastName)"/>
- </column>
How the Code Works
- <xsl:value-of select="..."/>: This tells the system to output the result of an expression.
- concat(FirstName, ' ', LastName): This is the core function. It takes the value of the <FirstName> node, adds a literal space ' ', and then adds the value of the <LastName> node, joining them together into a single string.
Resulting Target XML
- <importedData>
- <row>
- <column header="FullName">Jane Doe</column>
- </row>
- </importedData>
This pattern shows how to set the value of a column based on a condition.
Goal
Create a column named Status. If the source IsActive node is "true", set the value to "Active". Otherwise, set it to "Inactive".
Source XML (from a JSON file)
- <root>
- <element>
- <StudentID>101</StudentID>
- <IsActive>true</IsActive>
- </element>
- <element>
- <StudentID>102</StudentID>
- <IsActive>false</IsActive>
- </element>
- </root>
XSLT Stylesheet
This snippet goes inside a <row> element.
- <column header="Status">
- <xsl:choose>
- <xsl:when test="IsActive = 'true'">
- <xsl:value-of select="'Active'"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="'Inactive'"/>
- </xsl:otherwise>
- </xsl:choose>
- </column>
How the Code Works
- <xsl:choose>: This element starts a block of conditional choices, similar to an if/else statement.
- <xsl:when test="IsActive = 'true'">: This is the if condition. The test attribute checks if the value of the <IsActive> node is equal to the string 'true'.
- <xsl:value-of select="'Active'"/>: If the test passes, this line outputs the literal string 'Active'.
- <xsl:otherwise>: This is the else block. It catches any case where the <xsl:when> test fails.
- <xsl:value-of select="'Inactive'"/>: This line outputs the literal string 'Inactive' if the condition is not met.
Resulting Target XML
- <importedData>
- <row>
- <column header="Status">Active</column>
- </row>
- <row>
- <column header="Status">Inactive</column>
- </row>
- </importedData>
This pattern shows how to process a nested structure to create the <instance> nodes required for a repeater.
Goal
Convert a nested list of contacts from a JSON file into repeater instances.
Source XML (from a JSON file)
- <root>
- <element>
- <StudentID>4876</StudentID>
- <EmergencyContacts>
- <ContactName>Maria Garcia</ContactName>
- <ContactPhone>555-0101</ContactPhone>
- </EmergencyContacts>
- <EmergencyContacts>
- <ContactName>Luis Garcia</ContactName>
- <ContactPhone>555-0102</ContactPhone>
- </EmergencyContacts>
- </element>
- </root>
XSLT Stylesheet
This snippet goes inside a <row> element.
- <column header="ContactNameRepeater">
- <xsl:for-each select="EmergencyContacts">
- <instance>
- <xsl:value-of select="ContactName"/>
- </instance>
- </xsl:for-each>
- </column>
- <column header="ContactPhoneRepeater">
- <xsl:for-each select="EmergencyContacts">
- <instance>
- <xsl:value-of select="ContactPhone"/>
- </instance>
- </xsl:for-each>
- </column>
How the Code Works
- This pattern uses two separate <column> elements, one for each field in the repeater.
- <xsl:for-each select="EmergencyContacts">: This starts a nested loop. For the current record, it finds every <EmergencyContacts> element and repeats the code inside for each one.
- <instance>: Inside the loop, this creates the required <instance> tag for each contact.
- <xsl:value-of select="ContactName"/>: This pulls the ContactName from the current <EmergencyContacts> element being processed in the loop. The process is repeated for the ContactPhone.
Resulting Target XML
- <importedData>
- <row>
- <column header="ContactNameRepeater">
- <instance>Maria Garcia</instance>
- <instance>Luis Garcia</instance>
- </column>
- <column header="ContactPhoneRepeater">
- <instance>555-0101</instance>
- <instance>555-0102</instance>
- </column>
- </row>
- </importedData>
Related Articles
Understanding the Source and Target XML Formats
This guide explains the two critical data structures you need to know for any transformation: the system-generated source XML (your starting point) and the required target XML (your goal). Understanding the Source and Target XML Formats Every data ...
Introduction to Import Data Transformation
Sometimes, the data in your source file isn't in the exact structure or format that your forms require. Data Transformation is an optional but powerful step in the import process that allows you to clean, restructure, and manipulate your source data ...
An Introduction to XSLT
XSLT 1.0 (Extensible Stylesheet Language Transformations) is a versatile language designed for transforming XML data from one structure to another, supporting formats like CSV, JSON, and XML. As a core part of the W3C’s XML family of technologies, ...
Import Error Code Reference
When an import fails, the system will log an error code to help you diagnose the problem. This guide explains what each error code means and provides step-by-step instructions on how to resolve the issue. E0001: Cannot Read File Problem The system ...
XSLT Functions
XSLT (Extensible Stylesheet Language Transformations) provides a wide variety of built-in functions for working with data, manipulating strings, and performing calculations. Additionally, the we've added some important functions from the EXSLT ...