An easy to implement best practice to automate verification of XML files that saves time, reduces coding effort and supports easy maintenance.

In designing applications, you should be using XML as your data transport mechanism because most languages provide a robust set of tools for handling XML. Verification of XML data is the most common challenge which calls for a centralized verification mechanism.

General solution for verification of XML data is,

  1. Collect XML data to Hash Map (Hash map is the best solution to collects grid/list data as it stores key value pair combinations)
  2. Collect source data to Hash Map (Source either grid/list from application or Database)
  3. Compare both map objects

Collecting data to a map from .txt/.csv file is pretty easy as it contains comma separated values. However, collecting XML data to hash map is complicated. Why it is complicated? Because XML documents forms a hierarchical tree structure that starts at root element, contains multiple levels of child elements and each level may contain different attributes.

This complexity can be avoided by using XSLT to automate verification of data!

What is XSLT?

XSLT (Extensible Stylesheet Language Transformations) is the recommended style sheet language for XML.

An XSL stylesheet processor accepts a document or data in XML and an XSL stylesheet and produces the presentation of that XML source content that was intended by the designer of that stylesheet. With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

XSLT uses XPath to find information in an XML document.

By using XSLT, XML file can be converted to an output file types like .html, .csv, and .txt etc.

How to verify XML data using XSLT?
  1. Convert XML to any file type like .csv or .txt
  2. Collect converted file data to Hash Map
  3. Collect source data to Hash Map
  4. Compare both map objects

Now the question is why to favor the use of XSLT over other web technologies.

One big reason is that XSLT can be used along with most other web technologies: Delphi’s WebBroker and WebSnap, ASP.NET, Java, Python, and most other solutions. Even JavaScript has XSLT processors. So XSLT can be considered a fully cross-platform and cross-programming-language layer. This means that even if learning it is not trivial, your investment is likely to be preserved. Even though one extra step is added in ‘with XSLT ‘approach, it is easy to implement, time saving, reduces coding effort and supports easy maintenance.

Advantages of verification automation:
  • No code ambiguity, each XML documents can be a separate XSL file
  • Easy maintenance, any future changes in XML, output can be altered by simply modifying the transformations in xsl file
  • Code complexity is reduced by a lot as there is no need to change any code
Sample code to create .html/.csv/.txt from XML file

XML File to be automated:

<?xml version=’1.0′ encoding=’UTF-8′?>
<Envelope xmlns=””>
<QueryResponse xmlns=””>
<ABCFixes market=”16/17 Annual Auction” round=”1″>
<ABCQuote trade=”Buy”>
<Path source=”AECO” sink=”PECO” />

After developing XSLT file by using the above XML File tags, the final XSLT looks as below:

<?xml version=”1.0″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”” xmlns:fo=”” >
<xsl:output method=”text” omit-xml-declaration=”yes” indent=”no”/>
<xsl:template match=”/”>
<xsl:for-each select=”//ABCQuote”>
<xsl:for-each select=”//ABCFixes”>
<xsl:value-of select=”@market” />
<xsl:value-of select=”‘,'” />
<xsl:value-of select=”@round” />
<xsl:value-of select=”‘,'” />
<xsl:value-of select=”@trade” />
<xsl:value-of select=”concat(‘,’ , ID,’,’,Path/@source,’,’,Path/@sink,’,’,Class, ‘,’, Period, ‘,’, Hedge, ‘,’, MW, ‘,’ , Price,’&#xA;’

Create a java method which takes XML and XSLT files as input and return a file (here it returns .csv type

public File convertXMLTOCSV(String xslFileName, String xmlFile) {
File csvFile = null;
try {
File stylesheet = new File(xslFileName);
File xmlSource = new File(xmlFile);;
Document document = DocumentBuilderFactory
treamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance()
Source source = new DOMSource(document);
csvFile = new File(“newCSVFileName”+”.csv”);
Result outputTarget = new StreamResult(csvFile);
transformer.transform(source, outputTarget);
return csvFile;

} catch (Exception e) {
return csvFile;

Different situations where you can implement this automation strategy:
  • Data with complex XML structure
  • XML data verification by comparing with grid data
  • DB and Grid data is to be compared with XML data for verification
  • Data file formats like HTML, CSV and txt is to be compared with XML data for verification