Select Page

Verification Automation of XML data using XSLT

Verification Automation of XML data using XSLT

August 4, 2017

by Innova Solutions

Verification Automation of XML data

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:

<?xml version=’1.0′ encoding=’UTF-8′?>
<Envelope xmlns=”http://schemas.xmlsoap.org/soap/envelope/”>
<Body>
<QueryResponse xmlns=”http://eabc.xyz.com/abc/xml”>
<ABCFixes market=”16/17 Annual Auction” round=”1″>
<ABCQuote trade=”Buy”>
<ID>134866749</ID>
<Path source=”AECO” sink=”PECO” />
<Class>OnPeak</Class>
<Period>All</Period>
<Hedge>Obligation</Hedge>
<MW>0.6</MW>
<Price>10</Price>
</ABCQuote>
</ABCFixes>
</QueryResponse>
</Body>
</Envelope>

After developing the 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=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format” >
<xsl:output method=”text” omit-xml-declaration=”yes” indent=”no”/>
<xsl:template match=”/”>
<xsl:text>Market,Round,Trade,ID,Source,Sink,Class,Period,Hedge,MW,Price&#xA;</xsl:text>
<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:for-each>
<xsl:value-of select=”@trade” />
<xsl:value-of select=”concat(‘,’ , ID,’,’,Path/@source,’,’,Path/@sink,’,’,Class, ‘,
’, Period, ‘,’, Hedge, ‘,’, MW, ‘,’ , Price,’&#xA;’)”/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

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.

Services

Digital Product Engineering

Cloud Services

Data & Analytics

AI and Automation
Cybersecurity
Modern Managed Services

Build Operate Transfer

Innova Orion GCC Services

Talent Solutions

Industries

Communications & Media

Government Solutions

Healthcare, Life Sciences,
and Insurance

Banking & Financial Services

Energy, Oil & Gas and Utilities

Hi-Tech

Retail & CPG
Manufacturing

Travel & Transportation and Hospitality

Partnerships

AWS

Automation Anywhere

Databricks

Google

IBM

Microsoft

Pega

Salesforce

SAP
ServiceNow

Snowflake

Uipath

Innovation @ Work

Blogs and Insights

Research and Whitepapers

Case Studies

Podcasts

Webinars & Tech Talks
US Employment Reports

Company

About Us

Leadership Team

Strategic Partnerships

Office Locations

Newsroom

Events

ESG

The Innova Foundation

Careers

Explore Open Positions

Life @ Innova Solutions

Candidate Resource Library

Let's Connect