In this chapter, we will cover the following recipes to transform data from one format to another:
At the heart of any solution built with SOA Suite is the transformation of data from one format to another. Within SOA Suite we have two explicit mechanisms to deal with this, namely XML Stylesheet Transforms (XSLT) or XPath Assigns. Transformation may also occur implicitly as a result of using a component or adapter, for example converting to/from Java formats when using Java components and adapters.
Within both the Mediator and BPEL components, we have the choice between using XSLT transformation and XPath Assigns. When we use an XPath Assign, we are manipulating the target XML document directly. This may be more efficient when we are adding or altering a small part of the target document and the target document already contains data. When we use an XSLT transform, we are replacing the target XML document with a new one generated from the transform.
Choosing between Assign and Transform
Generally when we are populating a new variable for the first time, it is best to use a transform. A transform is also easier to use when manipulating repeating elements in the target. When we wish to modify only a few elements in the target, Assign is the easiest way to do this.
A common pattern is to perform an initial mapping to a variable by using XSLT and then making subsequent additions or modifications using Assign. The reverse pattern cannot be used because an XSLT transform will completely replace a variable with new content, losing any previous assignments to that variable.
Although XML Schema allows for element contents to be typed, generally the data transformation process is quite lax about enforcing type constraints, and as long as the data being transformed is compatible in its string form with the target type, no errors will be thrown; for example, the string 75
may be assigned to a numeric type without error, and similarly a numeric type such as 12.5
may be safely assigned to a string.