OL Learn

Open XSLT plugin -How to use it?-

Hello OL users,

Hope everyone is well during this tough times.

Going deeper and deeper in this complex software, I have identified the plugin mentioned above, which is a plugin I might need in the near future, for designing a workflow process for a new client. I’m really interested if someone could explain and help me with the following situation.

The data input, represent a various number of xml files for the same job. The trick that i have to do is to merge all the xml files before continuing with the process.

I know it was discussed here, but haven’t been able to decipher the syntax of XSLT .

Any help would be much appreciated !

Regards,
fsh22

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… :stuck_out_tongue:

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. :stuck_out_tongue:

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!

@Phil, the one and only!

You saved me big time!!! I’ve felt it’s not easy after finding the documentation on https://www.w3.org/TR/1999/REC-xslt-19991116.

Maybe I was still bonking my head over the desk, trying to make it work…

Somehow, I was near close to your example, after reading this, but I wasn’t aware of the folder listing trick. Luckily, your script did not require to many modification.

Thank you very much for taking your time, explaining this very basic concepts. I’m sure it is going to useful for other users who are going to need to work with Open XSLT plugin.

PS: It works like a charm!!!

Hats off!

Regards,
fsh22