Problem: I had a list of XML elements like so:
<Messages>
<Message Area="RES" Code="Code1"/>
<Message Area="RES" Code="Code2"/>
<Message Area="RES" Code="Code3"/>
</Message>
This is the XSD:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://danytests.com/soa/bpel-append"
targetNamespace="http://danytests.com/soa/bpel-append" elementFormDefault="qualified">
<xsd:element name="Messages" type="MessagesType">
<xsd:annotation>
<xsd:documentation>A sample element</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:complexType name="MessagesType">
<xsd:sequence>
<xsd:element name="Message" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="Area" type="xsd:string"/>
<xsd:attribute name="Code" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
What I needed to do was append more items to the already existing list without affecting the ones that were already in there.
At first what I was doing was adding the new items to add to a BPEL variable (
$varMessages) that was also of
MessagesType. Then, with another variable (
$messageCount), I would assign the count of
Messages/Message PLUS 1 to it (so, in this case, it would be 4). In a foreach loop, starting with
$messageCount, I would do a simple Copy to
Messages/Message[$messageCount] and setting the
insertMissingToData flag. This, however, isn't really the right way to go about this.
Solution:
Use the BPEL Append action instead of the Copy action.
To accomplish this, I would still have the
$varMessages variable assigned with the new items to add, but instead my BPEL assign activity would look like this:
<assign name="appendMessages">
<extensionAssignOperation>
<bpelx:append>
<bpelx:from>$varMessages/ns1:Message</bpelx:from>
<bpelx:to>$outputVariable.payload</bpelx:to>
</bpelx:append>
</extensionAssignOperation>
</assign>
That is, I'd assign the Messages from
$varMessages to the final list (
$outputVariable) in the Messages complexType.
The results were as expected while looking at it in the audit trail. First I'd assign a Message element to the $outputVariable:
Then I'd add elements to
$varMessages:
Finally, after the append action, the output variable's final list would look like this:
Neat!