File size: 3,063 Bytes
4724c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Script Task which uses XSLT

> Note: This example works also only with the XSLT scriptengine extension which is part of the
> Camunda Platform **enterprise edition**.

This quickstart demonstrates how to use the XSLT script engine extension to transform data in Camunda Platform.
The example is *classless*, relying entirely on scripting and expression language.

The example includes a BPMN 2.0 process which invokes a simple script task which transforms the input data
into an transformed XML output:

![XLST Example Process][1]

# Overview

- [bpmn source code][2]
- [xslt stylesheet][3]
- [input data][4]
- [result][5]
- [unit test][6]

## Requirements

* Java 17+

# How it works

The execution listener attached to the start event references a groovy script.

```xml
<bpmn2:startEvent id="StartEvent_1" name="give input">
  <bpmn2:extensionElements>
    <camunda:executionListener event="end">
      <camunda:script scriptFormat="groovy" resource="org/camunda/bpm/example/xsltexample/readXmlFile.groovy" />
    </camunda:executionListener>
  </bpmn2:extensionElements>
</bpmn2:startEvent>
```

This groovy script loads the [example XML file][4] and saves it in the process variable `customers`.

```groovy
import org.camunda.commons.utils.IoUtil

xmlData = IoUtil.fileAsString('org/camunda/bpm/example/xsltexample/example.xml')
execution.setVariable('customers', xmlData)

println 'Input XML:'
println xmlData
```

The following script task uses the [xsl stylesheet][3] to transform the XML saved in the `customers` process variable
and saves the result in the `xmlOutput` process variable.

```xml
<bpmn2:scriptTask id="ScriptTask_1" name="convert input" scriptFormat="xslt" camunda:resource="org/camunda/bpm/example/xsltexample/example.xsl" camunda:resultVariable="xmlOutput">
  <bpmn2:extensionElements>
    <camunda:inputOutput>
      <camunda:inputParameter name="camunda_source">${customers}</camunda:inputParameter>
    </camunda:inputOutput>
  </bpmn2:extensionElements>
</bpmn2:scriptTask>
```

Before the process ends the execution listener of the end event again executes a groovy script.

```xml
<bpmn2:endEvent id="EndEvent_1" name="show output">
  <bpmn2:extensionElements>
    <camunda:executionListener event="start">
      <camunda:script scriptFormat="groovy" resource="org/camunda/bpm/example/xsltexample/printResult.groovy" />
    </camunda:executionListener>
  </bpmn2:extensionElements>
</bpmn2:endEvent>
```

This groovy script just prints the process variable `xmlOutput` which contains the transformed XML.

```groovy
println '\nTransformed XML:'
println execution.getVariable('xmlOutput')
```

# How to use it?

1. Checkout the project with Git
2. Read and run the [unit test][6]

[1]: src/main/resources/xslt-example.png
[2]: src/main/resources/xslt-example.bpmn
[3]: src/main/resources/org/camunda/bpm/example/xsltexample/example.xsl
[4]: src/main/resources/org/camunda/bpm/example/xsltexample/example.xml
[5]: src/test/resources/expected_result.xml
[6]: src/test/java/org/camunda/bpm/example/xsltexample/XsltExampleTest.java