XML data is increasingly prevalent in the world of industrial automation. System vendors provide options for data interchange through XML, and industry organizations adopt XML as a standard for data transport, such as the OPC Foundation’s OPC XML-DA specification. Still, many front-line automation engineers find no benefit to old data restructured in XML alone. The benefits they seek may be found in technologies designed to support, consume and manipulate XML data. One such technology from the World Wide Web Consortium is a language specifically designed for transforming or restructuring an XML document into something else. The eXtensible Stylesheet Language Transformations (XSLT) is a simple, yet powerful language that can bring enormous benefit to those willing to learn it.
In this article, I will introduce the basics of XSLT through an example derived from an actual customer request. My coverage of the topic will be limited by the space available in this format however, I hope to awaken interest in a technology that might otherwise be overlooked and thereby give to some, a hint of the benefits waiting to be uncovered.
My customer maintains a large DeltaV automation system in an FDA-qualified environment. The customer would like a tool that simplifies daily demands of managing a large configuration. Such a tool, we have determined, is most easily developed around a relational database. Therefore we must transfer the configuration from control system to database in a structure that best supports the requirements of this tool.
A prototype of this tool is under development using Microsoft Access 2003 because of its built-in features for reporting and rapid application development. One such feature is the ability to import XML data through the File menu (File/Get External Data/Import), allowing the application of an XSLT transform before importing. This feature imports element-normal XML data by creating new tables with structures based upon those found in the XML document. The user optionally may choose to append data to existing tables following a set of mapping rules that match table and field names to element names in the XML document.
|FIGURE 1: THE XSLT TRANFORMATION PROCESS|
Restructure XML data or transform XML into an alternate text format.
Unfortunately, the XML structure of a DeltaV configuration does not match the desired data structures to meet our analysis requirements, so the raw XML data must be restructured or transformed into a different XML structure better suited to the customer’s requirements. Before learning the benefits of XSLT, I would have solved this problem by writing VBA code to load the XML document and programmatically walk the tree of nodes, adding new records to database tables using either the DAO or ADO object models. The XSLT solution presented here requires less code, is more robust, easier to develop, easier to maintain and modify, and is significantly faster than the VBA solution.
The XSLT style sheet restructures the XML for importing control modules and their parameters (See Sample Files). By dissecting this style sheet, I will explain basic XSLT concepts, while explaining solutions to common data structure problems.
Before reading on, get familiar with the basic structure of a DeltaV XML file. Notice that each control module is defined in a <module> element that has attributes for tag, plant_area, category, user and time. Also review the <attribute> and <attribute_instance> elements that are children of a <module> element.
Style Sheet as Transformation
The top element in an XSLT transformation must be the <xsl:stylesheet> element, and it must include a version attribute and the namespace declaration. An XSLT transformation is also an XML document, and therefore it must have a single top level element <xsl:stylesheet>. This element must include the namespace http://www.w3.org/1999/XSL/Transform, and it must have a version attribute. By convention, the namespace is generally assigned the xsl: prefix used throughout the document to distinguish XSLT language elements from literal output.
XSLT Transformation Processors
Execution of a transformation is performed by an XSLT processor that accepts XML and XSLT documents as input and generates a document as output. Output is created through execution of templates declared within the XSLT document. The processor walks the XML tree matching each node to a template in the XSLT document. If the processor cannot find a matching template in the XSLT file, it executes a built-in template inside the processor.