Right off the bat, let’s get something straight: XSLT is a monster!
No one in their right mind would want to spend time learning, even less mastering that arcane syntax… but hey, nobody ever said I was sane, so I’ll try and flex my XSLT muscles which have been at rest for a few years and see if I can explain some very basic concepts with the help of an example that’s similar to what you asked for… but after that, you’re on your own!
XSLT works by selecting elements and then applying transformations to those elements. To select elements, you use so-called templates and the selector inside the template is what allows it to match the elements you are targeting for transformation.
So lets say you have a simple XML file like this one:
<?xml version="1.0" encoding="ISO-8859-1"?>
To select the
CustomerID element with an XSLT template, you’d use something like:
<xsl:template match="RECORD/CustomerID" />
See how easy that was? Hold on, it’s gonna get much worse…
You can also tell XSLT to load external files and to use them in your transformation. You do that through the
document() selector. Let’s look at the example I mentioned earlier.
Say you have 3 XML files that you want to merge. First thing is to generate a list of those files in XML. We’re in luck because that’s exactly what the Folder Listing input task in Workflow does!
You’ll get a listing similar to this:
<?xml version="1.0" encoding="windows-1252"?>
<files count="3" filemask="Record*.xml">
Using that file, we can now write an XSLT that merges all files (assuming, of course, they all have the same structure). In this example, my 3 files have the following structure:
We are going to merge this into a new structure named
<RECORDS> which will contain all
<RECORD> elements from all 3 files.
Here’s the XSLT script (the code is commented with
<!-- comment --> statements, which you can safely remove):
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--The above statements are just standard declarations inside the XSLT-->
<!--First, match the ROOT element (/)-->
<!--Create the new RECORDS root element-->
<!--Apply the template to process each file (see below)-->
<!--Close the root element-->
<!--This template selects all FILE elements from the folder listing file.-->
<!--This returns a collection of elements over which the script iterates-->
<!--Concatenate the path and filename into a variable-->
<xsl:variable name="filename" select="concat(./path,./filename)" />
<!--Make a copy of each RECORD element in each target file-->
<xsl:copy-of select="document($filename)/RECORD" />
This resulting file will look like this:
<?xml version="1.0" encoding="UTF-8"?>
Pretty neat, right?
So anyway… XSLT is an extremely powerful language that you can use to transform an XML file any which way you want (you could even generate a CSV out of it). It is as powerful as another favorite of mine, Regular Expressions. Although… RegExes are, by comparison, a piece of cake to learn.
Yes, it’s daunting. No, it’s not fun… Well, it is fun, in a sad, twisted kind of way.
Hopefully it will help you get started. You’ll certainly have to do some reading to modify the script to do exactly what you want. So don’t say I didn’t warn you!