preload

XML/XSL Transformation: Stats Table

Posted by Albert Gareev on Mar 02, 2011 | Categories: ReportingSource codeXML Data

Parent page: Generating Test Reports

Today I present example of an HTML Table populated with XML data.
Here’s our XML record simulating stats data of a test session.

XML Source

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href=".\StatsTable.xsl"?>
<log>
  <description>Test login page</description>
  <reporter>John Doe</reporter>
  <timelog>
    <entry name="Start Date / Time">21/04/2011 11:39:24 AM</entry>
    <entry name="End Date / Time">21/04/2011 12:26:33 PM</entry>
  </timelog>
  <status>PASS</status>
</log>

Did you notice the following line?

<?xml-stylesheet type=”text/xsl” href=”.\StatsTable.xsl”?>

This is how we instruct the web browser to use XSL script for visualization of XML data.

Web-page Report

And here’s the source code of the script

XSL Source

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

<!-- variables -->
<xsl:variable name="CHAR_SPC">&#160;</xsl:variable>

<xsl:template match="/">

<html>

<head>
<title>automation-beyond.com | Stats Table XML/XSL Transformation Example by Albert Gareev</title>
</head>

<body bgcolor="#c0c0c0">

<H1 align="center">Stats Table</H1>
<H4 align="center">XML/XSL Transformation Example by <A HREF="http://automation-beyond.com/about/">Albert Gareev</A></H4>
<BR />
<P />
<h3 align="center">Test Stats</h3>
<BR />
<TABLE BORDER="1" width="650" align="center">

	<TR> <TH colspan="2" align="left">Overview</TH>       </TR>
	<TR> <TD width="200">Description</TD> <TD> <xsl:value-of select="log/description" /> <xsl:value-of select = "$CHAR_SPC" /></TD> </TR>
	<TR> <TD width="200">Reporter</TD> <TD align="center"> <B><xsl:value-of select="log/reporter" /> </B><xsl:value-of select = "$CHAR_SPC" /> </TD> </TR>

	<TR>
		<TD width="200">Execution Status</TD>
		<TD align="center"> <B>
            <xsl:choose>
		  <xsl:when test = " log/status = 'FAIL' ">
		    <span> <font color="red"> <xsl:value-of select="log/status" /> </font> </span>
		  </xsl:when>
		  <xsl:when test = " log/status = 'PASS' ">
		    <span> <font color="green"> <xsl:value-of select="log/status" /> </font> </span>
		  </xsl:when>
		  <xsl:when test = " log/status = 'OK' ">
		    <xsl:value-of select="log/status" />
		  </xsl:when>
		  <xsl:otherwise>
		    <span> <font color="yellow"> <xsl:value-of select="log/status" /> </font> </span>
		  </xsl:otherwise>
		</xsl:choose>
            </B> </TD>
	</TR>

	<TR> <TH colspan="2" align="left">Time Log</TH>       </TR>

	<xsl:for-each select="log/timelog/entry">
		<TR>
			<TD> <xsl:value-of select="@name" /> </TD>
			<TD> <xsl:value-of select="text()" /> </TD>
		</TR>
	</xsl:for-each>
</TABLE>
<BR />

<div align="center">
<A HREF="http://automation-beyond.com/chapters/tutorials/test-reports/">Home</A>
</div>
<BR />

</body>

</html>
</xsl:template>
</xsl:stylesheet>

How To

  • Define template of a table
  • Insert node values into table cells
  • To customize appearance use xsl:choose or xsl:if

 

And here is the package containing the original XML and XSL files.


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.