Interested in linking to "The ABCs of XML - Part 3: XSLT"?
You may use the Headline, Deck, Byline and URL of this article on your Web site. To link to this article, select and copy the HTML code below and paste it on your own Web site.
The final template creates a single <attribute> element in the output for each <attribute> child of a <module> in the source XML file. This template is a bit more involved, so I won’t dissect it line-by-line. Instead, I’ll describe what is accomplished by this template, and let you figure out how it is achieved. This template must include the following functionality:
The source XML document separates parameter definition (<attribute>) from its value (<attribute_instance>). This template must normalize this information into a single parameter element so the resultant parameter table in Access will include its value as well as its data type, category, position on the control diagram, etc. The template is applied to each <attribute> so a variable is created that points to an <attribute_instance> of the same name within the same <module> from which the value is output.
A <ModuleTag> element must be added to each <Parameter> as a foreign key for relating a parameter to its parent <module> in the database.
The XML for the value of an <attribute_instance> differs for different data types. For example, an integer type value 25 is stored as <value><cv>25</cv></value>, whereas a named set type is stored as <value><string_value>some string</string_value></value>. The transform must return the value no matter what the data type.
To support DeltaV named set data types, the output must include an <Enum> element that will contain the name of the named set found in the <attribute_instance>. This element must be included but empty for non-named set data types.
An attribute may be assigned multiple categories. A good relational design would have each category as a separate record in a related table. However, for simplicity sake, the category will be stored as a single field in the Parameter table as a comma-separated list.
The name and type attributes must be transformed to elements.
The parameter’s position on a control drawing defined by elements <x>, <y>, <h> and <w> must be transformed as children of the output <Parameter>.
Running the Sample
The sample files, including the results of the transformation, can be downloaded at www.controlglobal.com/XML3.html). Import the original XML file into MS Access both with and without the XSLT transformation to see the value of this transform and how well it performs.
Try this style sheet on your own DeltaV configuration—if you have one. If you have other XML data you would like to import into Access, try importing the data without a transform first. The XML structure may be suitable for import without a transform. If not, you have a great way to use what you’ve learned here and expand your reach with XSLT.
XSLT is a relatively easy language to master compared to a procedural language like VB. If you want to learn more you’ll find extensive online content and numerous publications. My personal favorite is XSLT Programmer’s Reference by Michael Kay from Wrox Press. The second edition covers XSLT 1.0 and 1.1. The third edition is also available which covers XSLT 2.0.
The final installment will provide more examples of using XSLT to transform XML data into something more useful.
Another standard from the W3C called XML Path Language (XPath) was developed for addressing parts of an XML document from within XSLT. XPath is a sort of query language for returning data subsets from an XML document that has its own syntax and rules like most languages. Within an XSLT stylesheet, an XPath expression may be used anywhere an XSLT element includes a select=”” attribute.
XPath expression look very similar to the pattern matching expressions in <xsl:template match=””> however the syntax and meaning of these expressions (match expression vs. select expression) is not the same. Most XSLT references provide a comparison of the differences between match expressions and select expressions otherwise known as XSLT Pattern Matching and XPath Expressions.
There are many XSLT processors available for download such as Saxon, Xalan and Microsoft MSXML. If you are working on a Windows XP computer, you already have a version of MSXML—most likely version 3 or version 4. All of the samples in this article were tested against MSXML4. The .NET framework also provides a .NET XSLT processor as well. Command line versions are available online for the XSLT processors mentioned here.
Different processors may produce slightly different results or may provide different extension functions that are not portable so to avoid problems it is a good idea to develop your transformations with a specific processor in mind.
|About the Author|
ControlGlobal.com is exclusively dedicated to the global process automation market. We report on developing industry trends, illustrate successful industry applications, and update the basic skills and knowledge base that provide the profession's foundation.