ggk-quote

Get A Quote

ggk-contact

+91 1234 44 4444

Blog

Verification Automation of XML data using XSLT

August 4, 2017

by Analysts

While designing applications, using XML as your data transport mechanism can be prudent as 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. The general solution for verification of XML data is,

  • Collect XML data to HashMap (a hash map is the best solution for collecting grid/list data as it stores key-value pair combinations)
  • Collect source data to Hash Map (the source can be a grid/list from either an application or database)
  • Compare both map objects

Collecting data to a map from a .txt/.csv file is pretty easy as it contains comma-separated values. However, collecting XML data to a hash map is complicated. Why it is complicated? Because XML documents form a hierarchical tree structure that starts at a root element and contains multiple levels of child elements and each level may contain different attributes. This complexity can be avoided by using XSLT to automate the 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?

  • Convert XML to any file type like .csv or .txt
  • Collect converted file data to HashMap
  • Collect source data to HashMap
  • Compare both map objects

Why XSLT?

Now the question is why should you favor the use of XSLT over other web technologies?

One big reason is that XSLT can be used along with most other web technologies like 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 that easy, your investment is likely to be preserved. Even though one extra step is added in ‘with XSLT ‘approach, it is easy to implement, saves time, reduces coding effort and supports easy maintenance.

Advantages of Verification Automation:

No code ambiguity, each XML document can be a separate XSL file

Easy maintenance, any future changes in XML, the output can be altered by simply modifying the transformations in the .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 the XML file:

XML File to be automated:







134866749

OnPeak
All
Obligation
0.6
10




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





Market,Round,Trade,ID,Source,Sink,Class,Period,Hedge,MW,Price








)”/>


Create a java method which takes XML and XSLT files as input and returns a file (here it returns the .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
.newInstance().newDocumentBuilder().parse(xmlSource);
treamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(stylesource);
Source source = new DOMSource(document);
csvFile = new File(“newCSVFileName”+”.csv”);
csvFile.createNewFile();
Result outputTarget = new StreamResult(csvFile);
transformer.transform(source, outputTarget);
return csvFile;
} catch (Exception e) {
e.printStackTrace();
}
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.