ASP.NET, Coding

How to Transform XML Data into a Table

With the advance of XML technologies it is often required to display the data in a XML file to a tabular form. Using xml data island concept this could be easily done for Internet Explorer. But for a common solution acceptable to all browsers we have look for XSLT or XML Style Sheet Language.

XSLT Syntax: It would be easier to explain the concept, if I start with an example outright, then we will move to the syntax of XSLT.
Let us consider the following xml file:

<?xml version = "1.0" encoding="ISO-8859-1"?>
<directory>
<employee>
 <name>James Fernandex</name>
 <title> Controller of Examination</title>
 <phone>033-33434459</phone>
 <email>jfernandez@democomp.com</email>
</employee>
.
.
</directory>
</xml>

To convert the file into a XHTML table we create an XSL Style Sheet (“staff.xsl”) with a transformation template:

<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:template match="/">
  <html>
  <body>
    <h2>Staff Directory</h2>
    <table border="1">
    <tr bgcolor="grey">
      <th align="left">Name</th>
      <th align="left">Position</th>
      <th align="left">Phone No</th>
       <th align="left">Email</th>
    </tr>
    <xsl:for-each select="directory/employee">
    <tr>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="phone"/></td>
       <td><xsl:value-of select="email"/></td>
      
    </tr>
    </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

After creating the XSLT we just add the XSL style sheet reference to our XML document (“staff.xml”):

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="staff.xsl"?>
<directory>
.<employee>
.
.
</employee>

The element <xsl:stylesheet> defines that this document is an XSLT style sheet document. An XSL style sheet consists of one or more set of rules that are called templates. Each template contains rules to apply when a specified node is matched.The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match=”/” defines the whole document).

The <xsl:value-of> element is used to extract the value of a selected node. The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.

The <xsl:for-each> element allows us to do looping in XSLT. We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element.

For example:

<xsl:for-each select="directory/employee[name='James Fernandez']">

Legal filter operators are:

    =  (equal)
    != (not equal)
     < less than
     > greater than