How to use XSL in Magento ?

Aitoc Team

How often do Magento users deal with massive data in the XML file? I think, the answer is clear: every time when they want to transfer Orders data between different versions of Magento and third party software. Let’s talk about the magic XSL Language.

What is XSL ?

XSL is an Extensible Stylesheet Language. The full XSL logically consists of 3 Components ( XSL,XSLT and XPath ), and today we speak about XSLT, which is the most important one. XSL Transformations or XSLT is a language for describing how to transform one XML document ( represented as a tree ) into another.

So to make your XML file more convenient for reading and information extraction, you really need Extensible Stylesheet Language. XSL actively uses stylesheets, which specifies the presentation of XML information using two basic categories of techniques:

* An optional transformation of the input document into another structure.

* A description of how to present the transformed information (i.e., a specification of what properties to associate to each of the various parts of the transformed information)

We introduce some examples for you to understand how it works.

XSL represents a number of instruments to rewrite your XML file. Let’s analyze them.

Use <for-each> instrument

<for-each> is a loop which processes each of the selected nodes using the template that it contains.

/Goal/ Imagine that you have a file with information about books that are available in stock, and you need a table that contains authors, titles, prices, publish dates and descriptions of all these books.

1. Let’s start with a basic XML file, which includes detailed information. This XML will be used to create an XSL template.

<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>
After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.
</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>
In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.
</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>
The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.
</description>
</book>
</catalog>

2. Now you see the XSL template in which <xsl:for-each select=”catalog/book> is used. Where select=”catalog/book” is an attribute. After your XSL template is created, apply it to a basic XML file.
 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="catalog/book">
<tr>
<td>
<xsl:value-of select="author"/>
</td>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="publish_date"/>
</td>
<td>
<xsl:value-of select="description"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

3. The final variant represents only needed information on the XML base, which was formatted according to the XSL template.

<table>
<tbody>
<tr>
<td>Gambardella, Matthew</td>
<td>XML Developer's Guide</td>
<td>44.95</td>
<td>2000-10-01</td>
<td>An in-depth look at creating applications with XML.</td>
</tr>
<tr>
<td>Ralls, Kim</td>
<td>Midnight Rain</td>
<td>5.95</td>
<td>2000-12-16</td>
<td>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</td>
</tr>
<tr>
<td>Corets, Eva</td>
<td>Maeve Ascendant</td>
<td>5.95</td>
<td>2000-11-17</td>
<td>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</td>
</tr>
<tr>
<td>Corets, Eva</td>
<td>Oberon's Legacy</td>
<td>5.95</td>
<td>2001-03-10</td>
<td>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.</td>
</tr>
<tr>
<td>Corets, Eva</td>
<td>The Sundered Grail</td>
<td>5.95</td>
<td>2001-09-10</td>
<td>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.</td>
</tr>
<tr>
<td>Randall, Cynthia</td>
<td>Lover Birds</td>
<td>4.95</td>
<td>2000-09-02</td>
<td>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.</td>
</tr>
</tbody>
</table>

4. The visual variant of the final code contains a table with the information about author, title, price, publish date and description for each book.

Gambardella, Matthew XML Developer’s Guide 44.95 2000-10-01 An in-depth look at creating applications with XML.
Ralls, Kim Midnight Rain 5.95 2000-12-16 A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
Corets, Eva Maeve Ascendant 5.95 2000-11-17 After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.
Corets, Eva Oberon’s Legacy 5.95 2001-03-10 In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.
Corets, Eva The Sundered Grail 5.95 2001-09-10 The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon’s Legacy.
Randall, Cynthia Lover Birds 4.95 2000-09-02 When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.
 

Use both <for-each> and <if> instruments.

/Goal/ Suppose, you have a file with information about books that are available in stock again. But now you need to present particular information only about Fantasy books, such as author, title, publish date and description, so we have particular conditions.

1. Here we use the basic XML file from the previous example.

2. To make our goal reality we create an XSL template with <xsl:for-each select=”catalog/book”> and <xsl:if test=”genre = ‘Fantasy'”>.  <if> is an instrument, which allows you to use particular condition.
 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="catalog/book">
<xsl:if test="genre = 'Fantasy'">
<xsl:value-of select="author"/>
presented
<xsl:value-of select="title"/>
and published it in
<xsl:value-of select="publish_date"/>
. Here is small book description:
<xsl:value-of select="description"/>
<xsl:text></xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

3. The final variant is represented as a plain text, according to our condition.

Ralls, Kim presented Midnight Rain and published it in 2000-12-16. Here is small book description:
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
Corets, Eva presented Maeve Ascendant and published it in 2000-11-17. Here is small book description:
After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.
Corets, Eva presented Oberon's Legacy and published it in 2001-03-10. Here is small book description:
In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.
Corets, Eva presented The Sundered Grail and published it in 2001-09-10. Here is small book description:
The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.

Use <xsl:choose>, <xsl:when> and <xsl:otherwise> instruments.

/Goal/ Let’s say, you have a file with information about books that are available in stock one more time. But now your goals are more complicated: you want to sort information, according to the genre. To divide various kinds of genres, a particular color will be used. For instance, fantasy genre will be violet, computer genre will be green and the other genres will be yellow.

1. Use the basic XML file again.

2.  In this XSL template we use <xsl:choose> instrument. <xsl:choose> is a kind of a multi-way switching element,  which includes more instruments: <xsl:when> and <xsl:otherwise>. All these elements are interrelated. The content of the first <xsl:when> element evaluates to true is inserted into the result tree. If none of them evaluate to true, the content of the <xsl:otherwise> element is inserted.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="catalog/book">
<xsl:choose>
<xsl:when test="genre = 'Fantasy'">
<tr>
<td style="background-color: #bd7aec">
Genre of:
<xsl:value-of select="genre"/>
<xsl:text></xsl:text>
<xsl:value-of select="author"/>
presented
<xsl:value-of select="title"/>
and published it in
<xsl:value-of select="publish_date"/>
. Here is small book description:
<xsl:text></xsl:text>
<xsl:value-of select="description"/>
<xsl:text></xsl:text>
</td>
</tr>
</xsl:when>
<xsl:when test="genre = 'Computer'">
<tr>
<td style="background-color: #62e789">
Genre of:
<xsl:value-of select="genre"/>
<xsl:text></xsl:text>
<xsl:value-of select="author"/>
presented
<xsl:value-of select="title"/>
and published it in
<xsl:value-of select="publish_date"/>
. Here is small book description:
<xsl:text></xsl:text>
<xsl:value-of select="description"/>
<xsl:text></xsl:text>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<td style="background-color: #fea">
Genre of:
<xsl:value-of select="genre"/>
<xsl:text></xsl:text>
<xsl:value-of select="author"/>
presented
<xsl:value-of select="title"/>
and published it in
<xsl:value-of select="publish_date"/>
. Here is small book description:
<xsl:text></xsl:text>
<xsl:value-of select="description"/>
<xsl:text></xsl:text>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

3. The final variant includes only needed information on the XML base, which was formatted, according to the XSL template.
 

<table>
<tr>
<td style="background-color: #62e789">
Genre of: Computer Gambardella, Matthew presented XML Developer's Guide and published it in 2000-10-01. Here is small book description: An in-depth look at creating applications with XML.
</td>
</tr>
<tr>
<td style="background-color: #bd7aec">
Genre of: Fantasy Ralls, Kim presented Midnight Rain and published it in 2000-12-16. Here is small book description: A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
</td>
</tr>
<tr>
<td style="background-color: #bd7aec">
Genre of: Fantasy Corets, Eva presented Maeve Ascendant and published it in 2000-11-17. Here is small book description: After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.
</td>
</tr>
<tr>
<td style="background-color: #bd7aec">
Genre of: Fantasy Corets, Eva presented Oberon's Legacy and published it in 2001-03-10. Here is small book description: In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.
</td>
</tr>
<tr>
<td style="background-color: #bd7aec">
Genre of: Fantasy Corets, Eva presented The Sundered Grail and published it in 2001-09-10. Here is small book description: The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.
</td>
</tr>
</table>

4. The visual variant of the final code is presented as a table and it is divided by colors, according to genres.

Genre of: Computer Gambardella, Matthew presented XML Developer’s Guide and published it in 2000-10-01. Here is small book description: An in-depth look at creating applications with XML.
Genre of: Fantasy Ralls, Kim presented Midnight Rain and published it in 2000-12-16. Here is small book description: A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.
Genre of: Fantasy Corets, Eva presented Maeve Ascendant and published it in 2000-11-17. Here is small book description: After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.
Genre of: Fantasy Corets, Eva presented Oberon’s Legacy and published it in 2001-03-10. Here is small book description: In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.
Genre of: Fantasy Corets, Eva presented The Sundered Grail and published it in 2001-09-10. Here is small book description: The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon’s Legacy.
Genre of: Romance Randall, Cynthia presented Lover Birds and published it in 2000-09-02. Here is small book description: When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.
Genre of: Romance Thurman, Paula presented Splish Splash and published it in 2000-11-02. Here is small book description: A deep sea diver finds true love twenty thousand leagues beneath the sea.
Genre of: Horror Knorr, Stefan presented Creepy Crawlies and published it in 2000-12-06. Here is small book description: An anthology of horror stories about roaches, centipedes, scorpions and other insects.
Genre of: Science Fiction Kress, Peter presented Paradox Lost and published it in 2000-11-02. Here is small book description: After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.
Genre of: Computer O’Brien, Tim presented Microsoft .NET: The Programming Bible and published it in 2000-12-09. Here is small book description: Microsoft’s .NET initiative is explored in detail in this deep programmer’s reference.
Genre of: Computer O’Brien, Tim presented MSXML3: A Comprehensive Guide and published it in 2000-12-01. Here is small book description: The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.
Genre of: Computer Galos, Mike presented Visual Studio 7: A Comprehensive Guide and published it in 2001-04-16. Here is small book description: Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.
 

To distinguish between <xsl:choose>  and <xsl:if> it should be said that we use <xsl:if>, when we have only one particular condition, but when we have more conditions, we use choose.

You can use XSL for two our modules:

* Orders Export and Import for Magento 1 and Magento 2, which is widely used for transferring data between Magento versions and third-party software.

* Smart Reports, which allows you to see your strong and weak sales points, user statistics, and other useful metrics at a glance. We create our vision of statistics in that module, but maybe you want to create your own and for this goal you can use XSL.

The example of “Orders Export and Import”

/Goal/ It’s obvious that you have cases when the extraction of particular data was essential to you. Now you export the XML file from Orders Export and Import extension. For instance, you have the XML file with all Entities and Fields, but you need only customer email, subtotal, grand total and increment id. We know that sometimes these fields and entities are confusing and that’s why we present the table, which introduces definitions of the most useful entities and fields.

1. The basic XML file, created by Orders Export and Import extension, includes detailed information about one order.

<orders>
<order>
<fields>
<increment_id>100000004</increment_id>
<created_at>
<![CDATA[ 2014-07-16 14:37:00 ]]>
</created_at>
<customer_email>
<![CDATA[ banderas@example.com ]]>
</customer_email>
<customer_firstname>
<![CDATA[ Antonio ]]>
</customer_firstname>
<customer_lastname>
<![CDATA[ Banderas ]]>
</customer_lastname>
<entity_id>6</entity_id>
<grand_total>872.0000</grand_total>
<order_currency_code>
<![CDATA[ USD ]]>
</order_currency_code>
<status>
<![CDATA[ complete ]]>
</status>
</fields>
<items>
<item>
<item_id>6</item_id>
<created_at>
<![CDATA[ 2014-07-16 14:37:00 ]]>
</created_at>
<product_id>166</product_id>
<product_type>
<![CDATA[ simple ]]>
</product_type>
<sku>
<![CDATA[ HTC Touch Diamond ]]>
</sku>
<name>
<![CDATA[ HTC Touch Diamond ]]>
</name>
<qty_ordered>1.0000</qty_ordered>
<price>800.0000</price>
<price_incl_tax>867.0000</price_incl_tax>
</item>
</items>
<addresses>
<address>
<region_id>43</region_id>
<postcode>10120</postcode>
<lastname>
<![CDATA[ Banderas ]]>
</lastname>
<street>
<![CDATA[ Attorney at Law 1556 Broadway, suite 416 ]]>
</street>
<city>
<![CDATA[ New York ]]>
</city>
<email>
<![CDATA[ banderas@example.com ]]>
</email>
<telephone>+651651654168</telephone>
<country_id>
<![CDATA[ US ]]>
</country_id>
<firstname>
<![CDATA[ Antonio ]]>
</firstname>
<address_type>
<![CDATA[ billing ]]>
</order>
</orders>

2. To obtain necessary entities and fields, create a template with the help of XSL language. To describe only needed entities and fields for each order, use  <xsl:for-each> instrument.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<orders>
<xsl:for-each select="orders/order">
<order>
<fields>
<xsl:for-each select="fields">
<customer_email>
<xsl:value-of select="customer_email"/>
</customer_email>
<subtotal>
<xsl:value-of select="subtotal"/>
</subtotal>
<grand_total>
<xsl:value-of select="grand_total"/>
</grand_total>
<increment_id>
<xsl:value-of select="increment_id"/>
</increment_id>
</xsl:for-each>
</fields>
<items>
<xsl:for-each select="items/item">
<item>
<store_id>
<xsl:value-of select="store_id"/>
</store_id>
<item_id>
<xsl:value-of select="item_id"/>
</item_id>
<sku>
<xsl:value-of select="sku"/>
</sku>
<name>
<xsl:value-of select="name"/>
</name>
</item>
</xsl:for-each>
</items>
</order>
</xsl:for-each>
</orders>
</xsl:template>
</xsl:stylesheet>

3. The final view of the code represents entities and fields, described in the XSL template, with the information from the basic XML file.
 

<orders>
<order>
<fields>
<customer_email>banderas@example.com</customer_email>
<subtotal>800.0000</subtotal>
<grand_total>872.0000</grand_total>
<increment_id>100000004</increment_id>
</fields>
<items>
<item>
<store_id>1</store_id>
<item_id>6</item_id>
<sku>HTC Touch Diamond</sku>
<name>HTC Touch Diamond</name>
</item>
</items>
</order>
</orders>

Entities Description
<orders>
<order>
<fields>
<increment_id> counting number of Order
<created_at> date of creation
<customer_email> email
<customer_firstname> firstname
<customer_lastname> lastaname
<entity_id> order’s identifier in the database
<grand_total> final cost of Order
<order_currency_code> currency, used in payment
<status> order’s status
</fields>
<items>
<item>
<item_id> counting number of Order’s item
<created_at> date of item’s creation
<product_id> counting number of product, using as Order’s item
<product_type> type of product
<sku> store keep unit
<name> product name
<qty_ordered> number of item
<price> price of item
<price_incl_tax> price including taxes
</item>
</items>
<addresses>
<address>
<region> region
<postcode> postcode
<lastname> lastname
<street> street
<city> city
<email> email
<telephone> telephone
<country_id> country identifier
<firstname> firstname
<address_type> type of address can be: Shipping, Billing
</address>
</addresses>
</order>
</orders>
 

Additionally, if you want to see all entities and fields you can use the Help file.

We are waiting for your feedback. Let’s express your ideas. We believe, it will be useful to you. We are ready to answer to all your questions and help you at any time.

Aitoc Team
The Aitoc team is a crew of distinguished Magento developers who have so far built dozens of top-performing Magento extensions. We do custom development for Magento eCommerce and other platforms!

Comments

comments