Liquid XML Studio
Cast
See Also Send Feedback
Data Mapping > Data Mapper Functions > By Category > Type > Cast

Glossary Item Box

Function Name
Cast
Category
Type
Icon
Cast
Description
Cast input data to a specified type
Inputs
Input Input value to cast
Outputs
Result Input value cast to specified type
Properties
Name Set a name for the function
Cast Type Choose a data type to cast the input value to

Usage

The Data Mapper Type Cast function returns the input value cast as a particular type. When mapping data, you naturally have to accommodate differing data storage models in the source and target applications. Converting from one type to another is therefore a common task. The Cast function can cast to various numerical types, booleans, Strings, binary/ bytes and date/ time types. To apply the Type Cast function, use the following process:

Create a new Data Mapper file, dragging your XML data source and targets into the editor area. For this example we are using client_source.xsd, an XML Schema Definition inferred from the following source:

Source XML Copy Code
<clients>
 <customer business="false">
  <name>Mary Smith</name>
 </customer>
 <customer business="true">
  <name>Steve Jackson</name>
 </customer>
</clients>

For our target schema we will be using client_target.xsd, inferred from the following XML:

Target XML Copy Code
<current_customers>
 <client>
  <is_business>0</is_business>
  <contact_name>Jim Brown</contact_name>
 </client>
 <client>
  <is_business>1</is_business>
  <contact_name>Jack Taylor</contact_name>
 </client>
</current_customers>

The data models clients for an organisation, with a boolean indicator of whether a client is in the "business" category or not. We are going to convert the boolean input values to integers, so that true is represented as 1, with false represented as zero. Here is the Data Mapper with source and target imported:

Data Mapper Source and Target

Drag the Type Cast function from the Component Palette into the mapping area:

Type Cast Component

Type Cast Transform Added

The Type Cast function takes a single input and gives an output of the type chosen. The input should be connected to the source item you want to cast to a specific type during mapping. The output should be connected to the target item, whether an item in the XML Writer or another component in the Mapper.

We need to let the Cast component know what type we want to cast to, so right-click it and choose Show Properties. In this case we choose the smallest integer type from the Cast Type drop-down list, since we only need to store values of 1 or 0. You can also alter the Name value to make your Cast function clearer as it appears in the Mapper, particularly useful if your Cast forms part of a large, complex mapping process.

Cast Properties

Notice that the drop-down list contains many different data types you can cast your input values to. Click OK to confirm your settings. Connect the "business" output in the XML Reader to the Cast input, and the Cast output to the "is_business" item in the XML Writer.

Cast to Reader and Writer

Finally, connect the remaining inputs and outputs:

Input and Output Connections Mapped

We can now execute the transform by pressing Shift-F5 or the Execute button (Execute). The transform is applied and the file we selected as output opens in the editor:

Output XML Copy Code
<current_customers>
 <client>
  <is_business>0</is_business>
  <contact_name>Mary Smith</contact_name>
 </client>
 <client>
  <is_business>1</is_business>
  <contact_name>Steve Jackson</contact_name>
 </client>
</current_customers>

The boolean items in the source have been cast to integers as part of the mapping process. The result is that the "business" status of a client is now represented numerically, using either zero or 1.

If you pass an input value to the Cast function that cannot be cast to your specified type, the Mapper will encounter an error. The function can handle the following data types: Binary, Boolean, Byte, DateTime, DateTimeOffset, Decimal, Double, Float, Guid, Int16, Int32, Int64, SByte, String, Time and Node.

This is a scalar function so the standard rules apply if multiple values are presented to one or more inputs.

See Also