During the initialization phase, the variable receives its initial XML content. This content can be subject to various manipulations during the BPEL process flow. We are able to instantly assign the content of the variable if the source of the data already exists. This recipe explains how to initialize a variable with the XML content.
We create a new synchronous BPEL process (VarInit.bpel
). We modify the request and response messages. The request message has the following schema:
<element name = "process"> <complexType> <sequence> <element name = "name" type = "string"/> <element name = "birthYear" type = "int"/> </sequence> </complexType> </element>
We need to provide the name and year of birth when starting the BPEL process instance. Similarly, we change the response message schema as follows:
<element name = "processResponse"> <complexType> <sequence> <element name = "firstName" type = "string"/> <element name = "age" type = "string"/> <element name = "responseTime" type = "dateTime"/> </sequence> </complexType> </element>
As a result of the BPEL process execution, we receive a name of the person, his/her age, and at what time the response from the BPEL process was sent.
The following steps show you how to initialize a variable with the XML literal content:
The XML literal must match the XML element definition in the XSD schema, so we must take care of that namespace as well as the tag names matching its definition.
All data in BPEL is represented in the XML format and so are all the variables. The types of variables are defined through the XML schema. The content of data in the variables is also presented in the XML data. So, whenever we perform an operation over a variable, we also perform an operation over the XML structure. By assigning an XML literal to a variable, we assign the XML content to the XML presentation of the variable.
In the BPEL process, where we assigned the XML literal to the variable, the following code appears:
<assign name = "Literal"> <copy> <from><processResponse xmlns = "http://xmlns.oracle.com/Variables/Global/VarInit"> <firstName>Jurij</firstName> <age>37</age> <responseTime>2013-04-15</responseTime> </processResponse></from> <to variable = "outputVariable" part = "payload" query = "/client:processResponse"/> </copy> </assign>
Another possibility is to partially initialize the variable. At some point during the BPEL process execution, we decide to initialize the variable with information we have and amend it later.
We modify the BPEL process by adding an additional assign activity. Now we have two assign activities in the BPEL process for the purpose of clarity. Let us modify the XML literal code in the Literal
assign activity to initialize only part of the variable. We only fill the firstName
element. The rest of the message consists of empty nodes as follows:
<copy> <from> <processResponse xmlns = "http://xmlns.oracle.com/Variables/Global/VarInit"> <firstName>Hello</firstName> <age/> <responseTime/> </processResponse> </from> <to variable = "outputVariable" part = "payload" query = "/client:processResponse"/> </copy>
The rest of the information can be filled in later with the assign activity.