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"?>
<RECORD>
<ID>1</ID>
<CustomerID>CU95086222</CustomerID>
</RECORD>
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">
<folder>C:\Tests\XSLT\
<file>
<filename>Record1.xml</filename>
<path>C:\Tests\XSLT\</path>
<time>2021/02/16 09:23:26</time>
<size>641</size>
</file>
<file>
<filename>Record2.xml</filename>
<path>C:\Tests\XSLT\</path>
<time>2021/02/16 09:23:48</time>
<size>652</size>
</file>
<file>
<filename>Record3.xml</filename>
<path>C:\Tests\XSLT\</path>
<time>2021/02/16 09:24:12</time>
<size>645</size>
</file>
</folder>
</files>
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:
<RECORD>
<ID>1</ID>
<CustomerID>xxxxxxxxxxx</CustomerID>
</RECORD>
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">
<xsl:output indent="yes"/>
<!--The above statements are just standard declarations inside the XSLT-->
<!--First, match the ROOT element (/)-->
<xsl:template match="/">
<!--Create the new RECORDS root element-->
<RECORDS>
<!--Apply the template to process each file (see below)-->
<xsl:apply-templates select="files/folder/file"/>
<!--Close the root element-->
</RECORDS>
</xsl:template>
<!--This template selects all FILE elements from the folder listing file.-->
<!--This returns a collection of elements over which the script iterates-->
<xsl:template match="files/folder/file">
<!--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" />
</xsl:template>
<!--All done!-->
</xsl:stylesheet>
This resulting file will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<RECORDS>
<RECORD>
<ID>1</ID>
<CustomerID>CU95086222</CustomerID>
</RECORD>
<RECORD>
<ID>2</ID>
<CustomerID>CU21510765</CustomerID>
</RECORD>
<RECORD>
<ID>3</ID>
<CustomerID>CU01429943</CustomerID>
</RECORD>
</RECORDS>
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!
Have fun!