Configuring a generic type in the local environment tree by using the Cast function

You can use the Cast function to define variables in a message map that are defined in the local environment tree, such as in the Variables folder.

About this task

Sometimes you need to use information that is passed in a generic type variable in the local environment or other part of the message assembly. For example, you might need to calculate the output value of a different element in the message body that is based on a value provided by a preceding node in the local environment variables folder, as shown in the following example:
LocalEnvironment.Variables.value_name
In this example, Variables is a generic element, and you must define the required elements in it for each value_name. You can also set values in the output local environment, Destination.specific_node, to override the behavior of a following node. For example:
  • For routing in the message flow, the map could set:
    LocalEnvironment.Destination.RouterList.DestinationData[1].labelName
  • To set a parameter to issue a REST request:
    LocalEnvironment.Destination.REST.Parameters.parameter_name
    In this case, Parameters is a generic element, and you must define the required elements in it for each parameter_name.
  • To set an order in a filter, to be used by a LoopBack request:
    LocalEnvironment.Destination.Loopback.Request.filter.order[1].name
    In this case, the order is a generic element, and you must define the required elements in it for each name.

Procedure

To configure the local environment tree Variables or other generic element, so that you can use its elements as part of your transformations, complete the following steps:

  1. If you already have the required element defined in a message model, such as an XML schema, DFDL schema, JSON schema, or Swagger document, omit this step and go to step 2; otherwise, complete this step to create a model to define your data.

    Create a schema file in your application, integration service, or library to define the elements of the local environment tree Variables folder and their type:

    • In the Application Development view, select New > Message Model > Other XML. Click Next.
    • Select Create an empty XML schema file, I will model my data using the XML schema editor, and then click Next.

      This figure shows the window that opens where you choose the option Create an empty XML schema file, I will model my data using the XML schema editor.

    • Create an XSD file YourLExsdFileName.xsd, where YourLExsdFileName is the name of the file that contains the local environment variables folder message model. Then, click Finish.

      This figure shows graphically the window that you use to select the project and enter the xsd file name.

    • The XSD file opens in a new tab where you use the XML Schema editor to define your variables and their types.

      For example, you can define the following schema:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
             <xsd:element name="Country"  type="xsd:string"/>
             <xsd:element name="CountryCode"  type="xsd:integer"/>
      </xsd:schema>
      Note: You can define the local environment variables in a message flow node by using ESQL or Java™. Namespaces is not defined. For this reason, the schema is also defined without a namespace declaration.
  2. Use the Cast function to define the local environment variables in the message map so they are visible under the Variables folder in the map. Complete the following steps to cast the any element to a variable and its type in the output local environment tree:
    • Right-click the any element, and then select Cast.

      This figure shows graphically the menu options that you choose to cast an element. You right-click the any element, and then select Cast.

    • In the Type Selection window, select a type, for example Country, and then click OK.
      This figure shows the Type Selection window with the elements Country and CountryCode within a red square.
      Note: Do not cast to an XML type that uses attributes. The data in the local environment tree is stored by using a generic parser and does not have specific field types set, (see XMLNSC: Using field types). In particular, if you copy data from an XMLNSC tree into the local environment tree, the field type that defines the element or attribute type is not preserved. If you model the data to be an attribute, the map attempts to access the data by using an XPath expression that matches data elements that have the field type that is specified in the XMLNSC tree, and so the data is not read.

Results

You have defined one local environment variable that can be used by other message flow nodes in your message flow for routing or filtering.

You can see the element Country under the local environment Variables folder in the message map.

This figure shows graphically the element Country under the any element of the local environment Variables folder.

Example

Another example:

If you set in an ESQL compute node two simple fields within the Variables folder of the Local Environment tree by using the following code:

SET Outputlocal environment.Variables.dec = 10.1;
SET Outputlocal environment.Variables.str = 'Some text';

To access these fields in a Mapping node by using the Cast function, you must create a schema file in your integration solution to define the elements and their type. Note that since the ESQL is not using any namespace to qualify these elements, the schema is also defined without a namespace declaration:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <xsd:element name="dec"  type="xsd:decimal"/>
       <xsd:element name="str"  type="xsd:string"/>
</xsd:schema>

Once the schema file is saved, you can then select the any element under the Variables section in the Local Environment tree, and use the context menu action Cast to add a Mapping cast for each of the elements "dec" and "str" that are required in the message map. For more information, see Mapping xsd:any on an input or output message.

What to do next

Define transforms between the input local environment tree and the output local environment tree. For more information, see Specifying a transform (mapping operation) and Transform types in the Graphical Data Mapping editor.