Modifying XML/HTML using XSLT in a GEL script

Blog Post created by nick_darlington Champion on May 11, 2015

A colleague in my team will be logging a new defect for an issue that has been found to occur in Clarity 14.2 when a GEL script in a process has a 'tag' with a 'src' attribute (even if that tag isn't part of the GEL directly, but part of the data in the GEL such as the content of a <gel:parse> or a <core:set> tag).


E.g. with a minimal example:


<gel:script xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary">

  <gel:parameter var="XOG_URL" default="http://test:123" />


  <gel:parse var="emailMsg">
    <img src="${XOG_URL}/niku/something/something" />


  <!-- do stuff -->



Attempting to save this in the UI is generating the following error on screen but no outputs in the Clarity (e.g. app-ca.log) log files:


    Fig. 1: "Error BPM-0703: Custom script syntax error at line 1, column 2: Reference is not allowed in prolog."


Note: The line/column indicators don't appear to change, it is not useful for helping identify the location of the problem in your script file.


Whilst there may be non-UI ways to work around this (for example it might be possible to overcome this by using XOG temporarily to apply changes to the process and scripts), it's not as convenient to an existing process than just changing the script and re-validating / re-activating through the UI.  It also wouldn't stop those errors from recurring if further changes were attempted by the UI.


So I went on the hunt for a workaround, and found we could use XSLT inside the GEL script to deal with this.  Here is a contrived example to deal with the above parsing, and although it may look heavy-handed at first, it's a very useful item to have in the toolbox for performing manipulation of XML/HTML for modifying, excluding, or even changing the format of some structured data to another format (also included in attached file demo_xslt_in_gel.txt below):




<gel:parameter var="XOG_URL" default="http://test:123" />


<!-- note that we've changed 'src' to 'rsc' compared to the original script -->

<gel:parse var="emailMsg">
  <img rsc="${XOG_URL}/niku/something/something" />


<!-- creating a variable to hold my transformation stylesheet -->
<!-- note that it contains 2 XSL templates; the first is a 'copy everything' template
     the second is one that intercepts - in our case - IMG tags and replaces the attribute
     from one that works (but is invalid, just a placeholder) called rsc to src with
     the same value -->
<gel:parse var="src_xsl">
<xsl:stylesheet xmlns:xsl="" version="1.0">
<xsl:template match="@*|node()">
      <xsl:apply-templates select="@*|node()"/>


<xsl:template match="img" />
  <xsl:attribute name="src">
    <xsl:value-of select="@rsc" />


<!-- we have the variables recreated using the XML tag library's own parser -->
<x:parse var="xog_xsl"><gel:include select="$src_xsl" /></x:parse>
<x:parse var="emailMsg"><gel:include select="$emailMsg" /></x:parse>


<!-- now we invoke the XML taglib's transformation tag to perform the work of applying the
     stylesheet changes to the input, the result being emitted into the surrounding GEL
     parse call to have the data back into a format desired by the other GEL variable
     processing tags for further use in the script -->
<gel:parse var="src_corrected">
  <x:transform xml="${emailMsg}" xslt="${xog_xsl}" />


<!-- just outputting the results into a process instance message so we can see what this
     demo has done -->
<gel:set select="$src_corrected" var="xog_output" asString="true" />
<gel:log level="INFO" category="XML_DUMP">${xog_output}</gel:log>


<!-- do stuff -->




Once familiar with this approach, a chunk of the script can be easily copy/pasted into other scripts to perform this kind of work without having to rewrite it.  It also isn't intended to be a permanent workaround to the problem that was reported with the 'src' attributes, but until it is fixed it may help if this is encountered.


Now we have the script completing and the variable holding the data we wanted:


    Fig 2: "[XML_DUMP]<?xml version="1.0" encoding="UTF-8"?> <img src="http://test:123/niku/something/something"/>"


Depending on how you want the other attributes in the tag (IMG in this case) handled, you may want to modify the XSLT templates further in order to migrate or change them.  Due to this specific problem that has been reported, only 'src' needed to be 'renamed' in this manner, but the same approach can be applied to convert data and formats between different business systems / interfaces, or other tasks too.