preload

XML verification example (XSL, HTML)

Posted by Albert Gareev on Dec 02, 2009 | Categories: Source codeXML Data

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

Parent page: XSL introduction and references

Sample task

Verify “transactions” xml file consisting of 10-1000 records.
Only “debit” or “credit” record names are valid. Any other entries must be located and reported.
Additionally, identify and report all debit records with debit amount greater than 50.00

Sample XML


<?xml version="1.0" encoding="utf-8"?>
<transactions>
<debit> <amount>10.0</amount></debit>
<credit> <amount>55.0</amount> </credit>
<debit> <amount>120.0</amount> </debit>
<debit> <amount>25.0</amount> </debit>
<credit> <amount>5.0</amount> </credit>
<credil> <amount>15.0</amount> </credil>
<credit> <amount>15.0</amount> </credit>
<debit> <amount>60.0</amount> </debit>
<debit> <amount>50.05</amount> </debit>
<debit> <amount>75.0</amount> </debit>
</transactions>

As you can see, going even through just 10 records might take a significant time and become really boring task.
Now let’s apply XSL transformation instructions.

Sample XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<html>
<head> <title>Transactions</title> </head>
<body bgcolor="#c0c0c0">
<h3 align="center">Transactions</h3>
<TABLE BORDER="1" width="500" align="center">
<TR> <TH>Transaction Type</TH> <TH>Transaction Amount</TH> </TR>
<xsl:for-each select="transactions/*">

<xsl:choose>
<xsl:when test = " local-name() = 'debit' ">
<TR>
<TD> <xsl:value-of select="local-name()" /> </TD>

<TD>
<xsl:choose>
<xsl:when test = " ./amount/text() > 50.00">
<span> <font color = "red"> <xsl:value-of select="./amount/text()" /> </font> </span>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./amount/text()" />
</xsl:otherwise>
</xsl:choose>
</TD>

</TR>
</xsl:when>
<xsl:when test = " local-name() = 'credit' ">
<TR>
<TD> <xsl:value-of select="local-name()" /> </TD>
<TD> <xsl:value-of select="./amount/text()" /> </TD>
</TR>
</xsl:when>
<xsl:otherwise>
<TR bgcolor="yellow">
<TD> <xsl:value-of select="local-name()" /> </TD>
<TD>?</TD>
</TR>
</xsl:otherwise>
</xsl:choose>

</xsl:for-each>

</TABLE>
<BR />
</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=”transform.xsl”?>

Where “transform.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 all the records in a table.

Validation becomes a matter of seconds!


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.