preload

Text File Compare Report example (XML, XSL, HTML)

Posted by Albert Gareev on Dec 03, 2009 | Categories: Source codeText DataXML Data


Root page: Service Functions – XML (QTP, VBScript)

Parent page: XSL introduction and references

Related post: Text File compare in “WDIFF” style (QTP, VBScript, XML, XSL) – Instructions and XSL script

Sample task

After line by line (and word by word) text file comparison has been performed, XML log was produced.
Review the log, identify and report found issues.

Sample XML 

<output>
<head>
<file1>c:\temp\1.txt</file1>
<file2>c:\temp\2.txt</file2>
<linecount1>3</linecount1>
<linecount2>3</linecount2>
<linefailcount>1</linefailcount>
<wordfailcount>1</wordfailcount>
</head>
<body>
<text status="MISMATCH">
<line status="MATCH">
<word status="MATCH">
<src indent="">aaa</src>
<dest indent="">aaa</dest>
</word>
</line>
<line status="MISMATCH">
<word status="MISMATCH">
<src indent="">bbb</src>
<dest indent="">zzz</dest>
</word>
</line>
<line status="MATCH">
<word status="MATCH">
<src indent="">ccc</src>
<dest indent="">ccc</dest>
</word>
</line>
</text>
</body>
</output>

Sample XSL 


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="CHAR_SPC"> </xsl:variable>

<xsl:template match="/">

<HTML>
<HEAD>
<style type="text/css">
span.highlight
{
background-color:"CCFF66"
}
span.indent
{
color:"#c0c0c0"
}
</style>

<TITLE>Text File Compare Report</TITLE>
</HEAD>
<BODY bgcolor="#c0c0c0">

<H1 align="center">Text File Compare Report</H1>

<TABLE BORDER="1" width="650" align="center">

<TR><TD colspan="2" align="center"><h3>File Stats</h3></TD></TR>
<TR>
<TD align="right">Baseline file<xsl:value-of select = "$CHAR_SPC" /></TD>
<TD align="left"><xsl:value-of select = "$CHAR_SPC" /><xsl:value-of select="output/head/file1" /></TD>
</TR>
<TR>
<TD align="right">Lines<xsl:value-of select = "$CHAR_SPC" /></TD>
<TD align="left"><xsl:value-of select = "$CHAR_SPC" /><xsl:value-of select="output/head/linecount1" /></TD>
</TR>
<TR>
<TD align="right">Test file<xsl:value-of select = "$CHAR_SPC" /></TD>
<TD align="left"><xsl:value-of select = "$CHAR_SPC" /><xsl:value-of select="output/head/file2" /></TD>
</TR>
<TR>
<TD align="right">Lines<xsl:value-of select = "$CHAR_SPC" /></TD>
<TD align="left"><xsl:value-of select = "$CHAR_SPC" /><xsl:value-of select="output/head/linecount2" /></TD>
</TR>
<TR><TD colspan="2" align="center"><h3>Comparison Stats</h3></TD> </TR>
<TR>
<TD align="right">Mismatching Lines<xsl:value-of select = "$CHAR_SPC" /></TD>
<TD align="left"><xsl:value-of select = "$CHAR_SPC" /><xsl:value-of select="output/head/linefailcount" /></TD>
</TR>
<TR>
<TD align="right">Mismatching Words<xsl:value-of select = "$CHAR_SPC" /></TD>
<TD align="left"><xsl:value-of select = "$CHAR_SPC" /><xsl:value-of select="output/head/wordfailcount" /></TD>
</TR>

</TABLE>

<BR />
<BR />
<HR />
<BR />

<TABLE border="1" width="1000" align="center">
<xsl:for-each select="output/body/text/line" >
<xsl:variable name="RowNumber" select="position()"/>
<TR>
<TD width="50">
<xsl:value-of select = "$RowNumber" />
</TD>
<xsl:choose>
<xsl:when test="contains(@empty,'Src')" >
<TD>
<xsl:value-of select = "$CHAR_SPC" />
</TD>
</xsl:when>
<xsl:otherwise>
<TD>

<xsl:for-each select="word">

<xsl:choose>
<xsl:when test="@status = 'MISMATCH'" >

<span> <xsl:value-of select="src/@indent" /> </span>
<span> <xsl:value-of select="src/text()" /></span>

</xsl:when>
<xsl:otherwise>
<span> <xsl:value-of select="src/@indent" /> </span>
<xsl:value-of select="src/text()" />
</xsl:otherwise>
</xsl:choose>

</xsl:for-each>
</TD>
</xsl:otherwise>
</xsl:choose>
<TD width="50">
<xsl:value-of select = "$RowNumber" />
</TD>
<xsl:choose>
<xsl:when test="contains(@empty,'Dest')" >
<TD>
<xsl:value-of select = "$CHAR_SPC" />
</TD>
</xsl:when>
<xsl:otherwise>
<TD>
<xsl:for-each select="word">

<xsl:choose>
<xsl:when test="@status = 'MISMATCH'" >

<span> <xsl:value-of select="dest/@indent" /> </span>
<span> <xsl:value-of select="dest/text()" /></span>

</xsl:when>
<xsl:otherwise>
<span> <xsl:value-of select="dest/@indent" /> </span>
<xsl:value-of select="dest/text()" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</TD>
</xsl:otherwise>
</xsl:choose>

</TR>
</xsl:for-each>

</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

Now you need to go back to your XML file and add the following line as first in the file:

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

Where “showdiff.xsl” is a name of the sample XSL script presented above, and assuming you saved it on the same folder. If you stored it somewhere else you may use any (full or relative) path notation.

Relative path notation is useful when you have hierarchical structure of data folders. Full path notation is useful if your XML files could be stored anywhere on the computer.

Finally, double-click on your XML file, and IE (if set as a default viewer for XML files) will display summary and line-by-line difference tables.


  • One response to "Text File Compare Report example (XML, XSL, HTML)"

  • Erik
    30th April 2010 at 16:17

    Nice example, but I think using specialized tools, like Beyond Compare, is better.

    [Albert’s Reply.
    Sure, for analysis it’s better.
    My example demonstrates how to visualize comparison results on a web-page.]

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.