Magento 2 import XSLT transformation – how to import any XML to Magento 2

- E-Commerce, Magento 2

When working with the suppliers, you often come across custom XML table formatting. Such tables cannot be imported to Magento 2 without giving all product attributes particular names.

Same applies to the export. Your CRM system probably accepts a set format of XML files. So as other software you might be using.

That’s why we are introducing XSLT support for Improved Import and Export extension. To help you get the required XML formatting for any needs you might have.

About

Improved Import and Export extension for Magento 2 allows you to import data into your Magento 2 store using XML files. However, the extension cannot consider all possible formatting of the XML files. That’s why we have come to the following acceptable XML format:

  • XML 1.0
  • encoding: UTF-8
  • field separator: space
  • first row: column names
  • top-level root name: Items
  • XML record name: item

The latest release of Improved Import and Export extension added possibility to accept XML tables of any format with the help of XSLT editor. The editor can be found inside the jobs you create, it is job specific, meaning that for every task you can create a dedicated XSLT template.

For Magento 2 import, XSLT editor can be used to interpret custom-formatted XML files. Making such files Improved Import and Export compatible. For Magento 2 export, XSLT editor can be used to define the format of the output XML file. This can be particularly useful if you intend to use it somewhere else.

Buy Improved Import Magento 2 Extension

What is XSLT

XSLT stands for EXtensible Stylesheet Language Transformations. As the name suggests, it is a language for transforming XML documents. XSLT is used to transform one XML document into another, and not necessarily XML.

XSLT belongs to XSL(T) languages.

  • XSLT is used to transform XML documents.
  • XPath is used in XML documents navigation.
  • XQuery is used for querying XML documents.

It all started with XSL – an XML stylesheet language. You can think of it as a CSS for HTML. However, XML doesn’t use predefined tags to identify its entities, so each tag can mean different object or subject. Here where the XSL comes, it defines how each XML element should be displayed.

XSLT is the most important part of XSL. It tells XML document how to form entries, how to name them, which punctuation to use, and what document should look like in the end.

You can think of XSLT as a script for forming end XML document. The translation is done in a single step, which can be roughly illustrated as:

As you can see from the illustration above, you only need a raw XML document and an XSLT template. When the raw XML undergoes the XSLT translation, you get a new XML document.

In the transformation process, XSLT defines which parts of the raw document should match one or more conditions/templates. When such match found, XSLT transforms it into the new document.

If you require more information on XSLT, please visit:

There, at w3schools.com, you will find all necessary information about XSLT.

So, what the XSLT can do for you:

  • Add/remove elements to or from the document;
  • Rearrange and sort elements;
  • Perform tests;
  • Decide which elements to display;

XSLT can do much more for your new XML document, and the choice is really up to you and the developer you are working with.

How can I use it

To understand how you can take advantage of XSLT editor in Magento 2 import and export processes you should first identify your workflow.

Import use case

You are working with a supplier that sends you catalog updates in XML, and you probably can’t import this XML right away. XML formatting differs significantly, and Improved Import and Export extension cannot consider every case.

Now, having XSLT editor you can import the XML file that your supplier sends you. Just write or ask your developer to write proper XSLT translation, paste it into the XSLT editor, and run the job. The extension will interpret the XML for you.

For example, a file from Criteo has the following formatting:

Look through the file at GitHub

Now, if we compose a proper XSLT template, we can translate this very XML file into the format supported by Improved Import and Export extension.

Remember, you need to match the following formatting:

  • XML 1.0
  • encoding: UTF-8
  • field separator: space
  • first row: column names
  • top-level root name: Items
  • XML record name: item

Besides, you need to consider what attributes Magento 2 uses for identifying imported entity properties.

 

For the full list of attributes – refer to the dedicated article. The full list of articles can be found here:

Magento 2 import/export guide list

Having considered the XML table formatting and attribute names supported by Magento 2, we can compose a proper XSLT template.

Look through the file at GitHub

If you know how to interpret XML, you can see that in the XSLT we are mapping initial XML table values with the required.

Now, that we have an XML file and its interpretation in XSLT, we can compose an import job.

Composing an import job

In this post, we are providing basic Improved Import and Export workflow example. If you want to learn more about how to create an import job, read through the extension manual.

Step 1: create an import job

Navigate to your Magento 2 admin panel and proceed to System > Improved import/export > Import Jobs and click ‘Add New Job’ button.

Step 2: fill in general information and import entity type

Run through the general settings, select the imported entity and upload your XML table.

Step 3: paste XSLT template

Proceed to the ‘XSLT Configuration’ section and paste the composed XSLT template.

Then, click ‘Test’ XSL Template’ button. The extension will run a test to make sure the XSL template is composed properly. If the template is ok – proceed to run the job.

Step 4: run the job

Complete the rest of the settings and hit ‘Save & Run’ button.

That’s it. You have just imported a custom-formatted XML table to your Magento 2 store.

Export use case

Now, how can you benefit from the XSLT while exporting entities from Magento 2? The first thing comes to mind is synchronization with your CRM system. For example, if you need to track sales and new customers.

Your CRM probably supports a dedicated XML file format. With the help of XSLT, you can translate XML files Improved Import and Export extension gives to you into a format supported by your CRM system.

Composing an export job

Let’s compose a sample export job with reference to the Criteo file we have tried importing in the Import use case.

Step 1: create an export job

Navigate to your Magento 2 admin panel and proceed to System > Improved import/export > Export Jobs and click ‘Add New Job’ button.

Step 2: fill in general information and export entity type

Run through the general settings and select the exported entity.

Step 3: paste XSLT template

Proceed to the ‘XSLT Configuration’ section and paste the composed XSLT template.

Look through the file at GitHub

Then, click ‘Test’ XSL Template’ button. The extension will run a test to make sure the XSL template is composed properly. If the template is ok – proceed to run the job.

Keep in mind that Improved Import and Export allows using php. functions in the XSLT configuration. Just create a helper within a custom module and call method to update value or format or any php. side operation.

For example, XSLT configuration of a file with orders from Acumen should look like:
<xsl:element name=”OrderDate”>
<xsl:value-of select=”php:function(‘Firebear\CustomImportExport\Helper\AcumenOrderExport::toAcumenDate’, string(created_at))”/>
</xsl:element>.

Step 4: run the job

Complete the rest of the settings and hit ‘Save & Run’ button.

That’s it. You have just exported a custom-formatted XML table from your Magento 2 store.

How to compose XSLT template

To compose XSLT templates you need to understand how to use and interpret XML. For a regular Magento 2 store administrator, it is a hard task.

That’s why FireBear Studio offers assistance with composing XSLT templates. If you are not sure how to do it – feel free contacting our support team:

Send us a message

We can compose XSLT template for you!

You can get:

  • Assistance with installing and configuring Improved Import and Export extension for Magento 2;
  • Consultation on importing different Magento 2 entities;
  • Get any questions about automating import/export processes answered.

Buy Improved Import Magento 2 Extension