COMPANY
COMMUNITY
BLOG
HELP
MY ACCOUNT
EN PT ES

Imagen6210E

XSLT (eXtensible Stylesheet Language Transformation)

(03/12/2002-00:00)
XML brought about new languages that complement it. Among these languages, there is the XSL (eXtensible Stylesheet Language) whose main use is "showing" the data from XML files. By Gastón Milano, from ARTech's development team.

XML (eXtensible Markup Language) represents, for Web programming, what TCP/IP represented at a different time for the Web's connection, or what the HTML language meant for web surfing. The first XML specifications originated in the mid-90's (1995); XML has now become a standard format for information exchange on the Web. It is present in most of the current development-related undertakings, and it is also used as a language for reading configurations or for creating XML interfaces that interact with old systems.

The appearance of XML brought about the origin of a large number of languages that complement it. XSL (eXtensible Stylesheet Language) is one of these languages, whose main use is "showing" the XML files data, and which was created with two main objectives in mind:

1-Transforming XML files into other file types: XSLT (eXtensible Markup Language Transformation)
It was originally meant to carry out complex style operations such as generating a content table, indexes, etc.; basically, things that could not be carried out by the CSS (Cascade Style Sheet). However, nowadays, XSLT is used as a multi-purpose tool for processing XML files.
2-Obtaining a "printable" version of an XML file: XSL-FO (eXtensible Markup Language Formatting Objects)

XSL transformations used in GeneXus
The GeneXus transformations with XSL are used in several components nowadays:

1- The most well-known case is made up of all the GeneXus listings. Since our listings required more data and continuous UI (User Interface) changes as time went by, we decided to shift to a more flexible scheme. In this new scheme, GeneXus is in charge of generating XML for the information in the Knowledge Base, which is then presented to the user through XSLT files. In this case, the input is an XML and the output an HTML.
2- It is used in the Olimar version theme editor to generate, from an XML which specifies the theme, the Cascade Style Sheet corresponding to that particular theme.
In this case, the input is an XML and the output is a CSS.
3- WSDL Inspector: It is used for conversion from an external XML into a format that is known by GeneXus, also in XML. In this case, the input is an XML and the output an XML.

Moreover, XSLT is generally used for carrying out any type of XML-file processing.
 
Although XSLT and XSL-FO started out as two joint specifications, they were then split up. This article offers a brief introduction to XSLT and its applications.

We said earlier that XSLT enables XML file transformation: an XSLT file is used for specifying the transformation to be made on an XML file, for obtaining an HTML, XML, VML, txt, ini file, etc. Then, an XSTL processor is in charge of making the transformation.

An XSL file is an XML file made up of a set of templates that indicate which changes are to be made in a given set of nodes.

The XSL processors are included in most of the existing browsers and affect the way the XSLT is applied to the XML file.
On Internet Explorer, for instance, the xsl to be applied may be specified through an XML instruction in the incoming xml.

<?xml-stylesheet type="text/xsl" href="MyXSL.xsl" ?> < /SPAN>

This command makes the browser apply the transformation with  "MyXSL.xsl", before sending out the result, and then send the result to the browser's output.
In this case, the transformation is made on the client by taking advantage of the browser's capacity. Another alternative is not to rely on the client's having the XSL processor, make the transformation on the server, and then send the result to the client.

Let us see a simple example as an introduction to the language:

Imagine that there is a software company which has a set of products for sale. The product list should be accessible to suppliers and also be published in the company's web site.
The suppliers receive the data to be processed in the XML format; each one of them has an XSL in order to make the processing and transformation of the XML. Likewise, the software company has an XSL by default for those suppliers who do not wish to worry about the presentation of the data.
 
                  The XML used for the exchange is very simple:

<?xml version='1.0'>
<Products'>
   <Product id="GeneXus">
         <Name>GeneXus</Name> 
         <Version> 7.5</Version>
         <CodeName>Ceibo</CodeName>
   </Product>
   <Product id="GXQuery">
          <Name>GeneXus Query </Name>
          <Version>1.0</Version>
     </Product>
</Products>

One of the suppliers wishes to show an HTML in the following way:
 
Product Version
GeneXus  7.5
GXQUERY 1.0
 
Before plunging into the XSLT, we should know what it is that we have to generate in the HTML format. In this case, that would be:


 TABLE
           TR  TD   B  Product  /B   /TD   TD  Version  /TD   /TR
           TR  TD  GeneXus  /TD   TD 7.5 /TD   /TR
 TR  TD  GXQuery  /TD   TD 1.0 /TD   /TR
 /TABLE

In this case, the stylesheet is pretty simple because there are very few data to take from the XML.
                                
How is the XSL applied for generating the desired result?

As in every language, the same result may be obtained in several ways. In XSL there are two clearly different styles:

- Procedural: as in the example above, the XSLT processor receives, at all times, an indication on what templates to apply.
- Declarative: in this scheme, more templates are to be declared, and the processor is left with the task of template application.

Obviously, programming is never totally done in one scheme, but it is convenient to program, if possible, in the declarative scheme, and for cases when there is no other choice, just guide the processor. This is due to the advantages offered by declarative programming, which enables keeping the XSL code and reusing the templates.


Procedural Scheme:

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

<xsl:template match="/">
    <TABLE>
         <TR><TD> <B>  Product </B> </TD> <TD> <B> Version </B></TD>  </TR>
         <xsl: for-each select="Products/Product">
              <TR><TD><xsl:value-of select="."/> </TD> <TD><xsl:value-of select="."/>  </TD></TR>
          </xsl: for-each>
          <xsl: apply-templates mode="Row" />
    </TABLE>
</xsl: template>

</xsl: stylesheet>

This XSLT has a single template applicable to the root node, where the XSL processor receives an indication on what it has to print in that particular node's output.
Furthermore, nodes with the
"xsl" prefix show the XSL processor whether it has to carry out operations on the input XML. For instance, the for-each xsl shows that all the nodes marked with the "select" attribute are to be run through.  In the example above this means running through the products; therefore, when getting into the for-each, there is always a Product-type selected node.
The value-of xsl retrieves the value of a given node; if it is a terminal node, it retrieves its value, otherwise it retrieves the succession of all the values of its sheet nodes.
                                               
Declarative scheme

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

<xsl:template match="/">
    <TABLE>
         <TR><TD> <B>  Product </B> </TD> <TD> <B> Version </B></TD>  </TR>
         <xsl:apply-templates>
    </TABLE>
</xsl: template>

<xsl: template match="Products">
    <TABLE>
         <TR><TD> <B>  Product </B> </TD> <TD> <B> Version </B></TD>  </TR>
         <xsl:apply-templates>
    </TABLE>
</xsl: template>

<xsl: template match="Product">
              <TR><TD><xsl:value-of select="."/> </TD> 
              <TD><xsl:value-of select="."/>  </TD>
              </TR>
</xsl: template>

</xsl: stylesheet>

As we can see, there are three templates now, one for the root node (this one is always required and it represents the input for the processor), another for "Products", and another for "Product".

XSL processor at work
The processor gets to the "/" template and starts sending the specified characters to the output until it runs into the apply-templates/ xsl command, which makes it look for nodes in the xml, and if there are templates for them, to apply them. This way, it finds the "Products" node and it starts applying its template.
The same happens within the products template, where the indication is to apply templates again, which will make it apply the "Product" template for each "Product" node that is found.
Another alternative for obtaining the same result is making a Web Panel that will process the XML and generate a table.
                                  
Advantages of the solution with XSL
- Each supplier may customize the presentation's format.
- Regenerating is not necessary for obtaining changes in the UI.
- It is possible to obtain output formats, which are hard to find otherwise. e.g.: there are occasions in which a graphic, a stylesheet, etc., are desired.
- The XSLT processor is in charge of processing the XSL, and not our program. This means that many times, when we run through an XML and generate an HTML, what we are actually making is a special XSL.
                                                         
When is it convenient to use XSL?
There are times in which we receive an XML file and we wish to put the data in order and also to show them. In this situation, as well as in situations in which, for some reason, some type of transformation into data received in an XML is required, it is convenient to think of XSL as a solution. Likewise, if the data have to be transformed in different ways, depending on certain rules, according to type of device, type of supplier, type of presentation required (graphics, HTML, XHTML, Text), there is no doubt that the solution should be XSL.
                                                                                   
For complete information on the language: http://www.w3c.org/Style/XSL/

Related
XML Lists
GeneXus 7.5 in Chinese
GeneXus technical information as a Web Service
New forum on the use of XML