Sunday, July 24, 2016

What is XSL and How to generate it

XSL is similar to CSS, it is a language for expressing style of an XML document where as CSS is used to define the style of a web page.

By applying XSL transformation, the style and structure of an existing XML document can be changed. In practical scenario, XML transformation can be applied if the structure of XML generated by one system is not in the format expected by the other system.

Here I am depicting a simple example on how to create an XSL to convert the structure of XML given in the Table-1 to Table-2. 

Table-1 (input file)
<Students>
      <Student>
                 <Name>Prabheesh</Name>
                  <Age>15</Age>
                  <Class>10</Class>
                  <Physics>86</Physics>
                  <Chemistry>85</Chemistry>
                  <Maths>88</Maths>
       </Student>
       <Student>
                 <Name>Vaishnav</Name>
                  <Age>14</Age>
                  <Class>9</Class>
                  <Physics>90</Physics>
                  <Chemistry>92</Chemistry>
                  <Maths>83</Maths>
       </Student>
</Students>
                

Table-2 (output file)
<Students>
      <Student>
                  <PrimaryInformation name=”Prabheesh” age=”15” class=”10”/>
                  <Marks>
                            <physics>86</Physics>
                            <Chemistry>85</Chemistry>
                            <Maths passmark="70">88</Maths>
                   </Marks>
        </Student>
       <Student>
                     <PrimaryInformation name=”Vaishnav” age=”14” class=”9”/>
                  <Marks>
                            <physics>90</Physics>
                            <Chemistry>92</Chemistry>
                            <Maths passmark="70">83</Maths>
                   </Marks>
       </Student>
</Students>
  



First, let us create an XSL file template. If you are using Eclipse/RAD, right click on the project name (assuming project is already existing in your workspace) and choose New -> Other -> XML -> XSL option. Enter a name for the XSL file (eg: mySample.xsl).

This will create an XSL template as below.

<?xml version=”1.0” encoding”UTF-8”?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”>


</xsl:stylesheet>


Now, let us see how to create XSL for converting XML document given in Table-1 to the one given in Table-2.

In a high level, the difference between these 2 XML documents are as follows.

a) Name, age and class nodes in the input XML (Table-1) needs to be transformed to attributes of <Primary Information> node.
b) A new node <Marks> is introduced to hold only mark details.
c) An extra attribute "passmark" is added in <Maths> node, whose value is fixed across all the students.


Given below is the XSL code that I developed for this transformation.
 
<?xml version=”1.0” encoding”UTF-8”?>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”> 

<xsl:variable name=”passmark” select”’70’”/></xsl:variable>

<xsl:template match=”/”>
<Students>
<xsl:for-each select=”Students/Student”>
         <Student>
                  <PrimaryInformation>
                                   <xsl:attribute name=”name”><xsl:value-of select=”Name”/></xsl:attribute>
                                   <xsl:attribute name=”age”><xsl:value-of select=”Age”/></xsl:attribute>
                                   <xsl:attribute name=”class”><xsl:value-of select=”Class”/></xsl:attribute>
                  </PrimaryInformation>

                 <Marks>
                               <Physics><xsl:value-of select=”Physics”/></Physics>
                               <Chemistry><xsl:value-of select=”Chemistry”/></Chemistry>
                              <Maths>
                                           <xsl:attribute name=”passmark”><xsl:value-of select=”$passmark”/></xsl:attribute>
                                             <xsl:value-of select=”Maths”/>
                              </Maths>
                        </Student>
          </xsl:for-each>
 </Students>
</xsl:template>
</xsl:stylesheet>


Now, I will explain the purpose of using each of the xsl elements mentioned above.

xsl:variable:

This is used to define a variable in XSL and assign value to it. Similar to other programming languages, this variable name can be used in the remaining portion of the document display the value assigned to this variable.

In this example, the passmark that need to be displayed against all the students in the XML is 70. Hence in the XSL, we defined it as a variable with value as 70. To display the value of the variable, the following XSL code can be used.

<xsl:value-of select=”$passmark”/>


xsl:for-each:

This construct is used to iterate through the nodes in the XML document. In our example, it will iterate through each <student> node. Whatever code we are writing inside this xsl:for-each logic will be to process the child nodes of <student> node.


xsl:attribute:

This element is used to define an attribute of a node. the name fields refers the name of the node.

xsl:value-of:

This will print the value of the variable or field specified inside the select clause.
<xsl:value-of select=”Name”/> will take the value of "Name" node present inside "Student" node and print it in the output XML. In the example, values "Prabheesh","Vaishnav" are printed using this code.

<xsl:value-of select=”$passmark”/> will print the value of the variable passmark. To print the value of variable, we need to append $ sign in front of the variable name. If the variable is not already defined in the XSL file, the transformation will fail.


To generate the transformed output XML, right click on the XSL we have created (in eclipse/RAD) and select Run As -> XSL Transformation. It will display a pop-up with option to specify input and output XML file. Choose the file with the contents mentioned in Table-1 as input. Mention a new file name as output XML. Click Apply and Run. You can see that an output XML is created with the contents mentioned in Table-2.





No comments:

Post a Comment