Using Context in Message Mapping

Objectives

After completing this lesson, you will be able to:
  • Explain queues in message mapping
  • Explain the impact of context selection and deletion
  • Use the message-mapping debug process

Queues

A message mapping queue consists of all the values of a field in the data flow editor. This applies to source field, target fields, and any fields created by mapping steps. These queues are important tools for testing because you can display the source field values, target field values, and any other field values that the mapping step created in the message mapping as soon as test data is available.

Queues Used in the Mapping Process

  • Inbound queue

    Consists of data from fields in the source message.

  • Processing queue

    Consists of data from inbound queues that the message mapping template processes.

  • Result queue

    Consists of data for entry in fields in the target message.

Hint

You can debug individual steps in the message mapping process by displaying the queue for each function in the message mapping.

You can perform message mapping at runtime using queues. A queue contains all the values that relate to a field in the message mapping. There is a queue for each XML hierarchy level. If you create an XML instance in the test environment editor, you can display the various queues in the data flow editor.

Runtime Queues

  • Source structure

    A queue exists for each field in the source structure.

  • Input and result queues

    Standard and user-defined functions process the input queues and write the result of a calculation to a result queue. The result queue of one function can also serve as the input queue for another function.

  • Target fields

    A queue exists for each target field. The limitations of the target field determine which values from the target field queue are written in the target structure.

Note

The field name and the structure hierarchy in which a field exists determines the field. If two fields have the same name but occur at different levels (paths), two separate queues are created.

By using queues, you can process large XML messages quickly.

A Document Object Model (DOM) parser builds the entire XML tree for an XML file and uses excessive memory and CPU time. A queue uses a Simple API for XML (SAX) parser that only builds the values of a hierarchy level, resulting in faster processing.

Queues with Same Context

If a source structure contains more than one instance of the same elements (occurrence > 1), the context of a queue can contain multiple values. As long as the input queues have the same number of values for each context, the way the values are processed is clear cut, as shown in the figure, Queues with Same Contexts.

Queues with Different Context

In the figure, Queues with Different Contexts, the input queues for the concat function have a different number of values in the related contexts. If, for this reason, an input value from a queue is missing for a function, the mapping runtime simply uses the last value read from the queue.

Note

A queue is read from right to left, which means the values 1 and a are processed first.

The rule that applies to queues with different contexts also applies to generating functions, such as constants, which only generate one value when they are linked to a function. If an additional input queue has more than one value in a context, the mapping runtime reuses the last value that the generating function returned.

Message Mapping Context

The context of a field is a description of the structure in which the field occurs. A field can contain multiple contexts if the field exists in a series of structure levels in a hierarchical manner. If a field is part of a structure that can have multiple occurrences (occurrences > 1), this is referred to as a context change for the field.

If a field occurs more than once (occurrence > 1) within one structure, this does not constitute a context change because the field occurs more than once within the same structure (context).

Context Changes in XML Files

A context change within a hierarchy is shown in the relevant queue. For example, in the figure, Context Changes in XML Files, the field item occurs in the context of three nodes with the name A. This results in two context changes for the queue on the hierarchy level of the field item.

Complex Context Change – Example

The figure, Complex Context Change – Example, illustrates the concept of context change in more detail for a field person that uses the following contexts:

  • flat

    In the context of the first flat, Fix and Foxy are the people who live there. There are four flats in total and therefore, three context changes.

  • street

    In the context of the first street, Fix, Foxy, Jolly Jumper, and Lucky Luke are the residents. There are three streets in total and, therefore, two context changes.

  • city

    In the context of the first city, Asterix and Obelix are also residents. There are two cities in total and, therefore, one context change.

  • root

    In the context of the root element root, Valerian is also a resident. There is no context change in this context.

You can create and delete context changes with both standard functions and user-defined functions. This enables a direct influence on the hierarchy levels in the source message.

Context Selection and Deletion

You can use the standard functions RemoveContexts() and SplitByValue() in the mapping editor to manipulate contexts.

By using the RemoveContexts() function, you can delete all context changes in a queue.

The standard function SplitByValue() is the inverse function of RemoveContexts().

By using the SplitByValue() function, you can insert any of the following:

  • An additional context change after any value

  • Any value change

  • Any empty entry

You select any of these options by setting the relevant property in the function.

Hint

If values are not filled in open contexts, they are lost. Open contexts are generated when the parent node is created.

Impact of RemoveContexts() on Message Mapping

The figure, Impact of RemoveContexts() on Message Mapping, shows how the RemoveContexts() works in the following ways:

  • Example 1

    In this example, only the values for the source field item of the first node header are used. The mapping program creates the corresponding item target fields, and the contents of the source fields are copied. In this case, the mapping stops for the item field at the first context change because node A can only occur once in this example.

  • Example 2

    In this example, the context change is deleted by RemoveContexts(), which means the contents of all the item source fields are transferred to the queue without a context change. As a result, the transferred fields are all created as item target fields and their values transferred accordingly.

Impact of SplitByValue() on Message Mapping

The figure, Impact of SplitByValue() on Message Mapping, shows how the function SplitByValue() works in the following ways:

  • Example 1

    In this example, the SplitByValue() function is not used and therefore, four context changes are generated, as required. However, after the first context change, all four item source fields are saved as item target fields and then populated with the relevant strings.

  • Example 2

    In this example, the SplitByValue(Each Value) function is used and the function results in a context change after each source field item. Every target field item appears in an individual context new_context.

Explicit Context Selection

You can set the context of source fields explicitly in the data flow editor by using the context menu.

Hint

The term context menu refers to the menu that you navigate to by right clicking. The term context has two different meanings here.

The figure, Impact of Explicit Context Selection on the Message Mapping, shows the effect of explicit context selection on the sample message type TestContext. Depending on whether you select segment, parent, or TestContext as the context for the item source field, the queue has either 4, 1, or no context changes. Selecting a higher context level deletes the context changes located under this level. You can use this method to simplify the task of context selection for source fields and avoid multiple use of the standard function RemoveContexts() in the message mapping.

You can use the function RemoveContexts() in target field mappings that consist of function chains.

You can use the standard function exists() to determine whether a specific source field exists in the XML instance being processed. If it does, exists() returns true; otherwise, it returns false. This is useful, for example, if you want to map an optional source field to an obligatory target field. You can check whether the field (not the contents) actually exists and if necessary, transfer a constant instead.

For a field with an occurrence greater than zero (occurrence > 0), you can use the standard function copyvalue() to copy the individual values separately to multiple target fields.

If a source structure contains a field with the name addrDat and the field has three values, you can map these three values to the street, city, and zipcode target fields by using copyValue() and specifying an index, as follows:

  • /ns0:MT_B/street=CopyValue(/ns0:MT_A/addrDat, nnumber=0)
  • /ns0:MT_B/city=CopyValue(/ns0:MT_A/addrDat, nnumber=1)
  • /ns0:MT_B/zipcode=CopyValue(/ns0:MT_A/addrDat, nnumber=2)

Target Structure Generation

You can use the mapping editor to assign the fields of two XML structures to each other. Target structures do not exist at runtime. The mapping program creates target structures only during the execution of the mapping.

Structure Types that Determine How the Target Structure Looks after it has been Created:

  • Source structure

    The number and type of tags that exist in the source structure instance

  • Target structure

    The number of occurrences specified for the target fields in the target structure

Whether the mapping program creates a particular field in the target structure depends on the number of occurrences of the field in the target structure and whether a target field mapping has been defined for this field.

The target field mappings required for fields in the target structure are as follows:

Target Structure Generation

OccurrenceConditions for Creation
n = minOccurs = maxOccurs > 0
  • The message mapping creates these structure fields automatically because they must occur n times in the target structure, independent of the target field mapping.

  • You can still assign a target field mapping to structure fields of this type.

minOccurs = 0The structure field is optional and is only created if you have assigned it a target field mapping.

The following applies here:

  • When you assign a target field mapping to the structure field, you also have to do this for mandatory attributes; otherwise, the message mapping is incomplete.

  • To create optional structure fields, it is not sufficient to create a target field mapping for attributes. The message mapping suppresses optional structure fields without target field mappings, including subnodes.

0 < minOccurs<maxOccurs
  • You must define a target field mapping for structure fields of this type.

  • The frequency with which structure fields are created depends on minOccurs, maxOccurs, and the target field mapping.

Note

Before a node with minOccurs = 0 can be created in the target structure, the message mapping must contain a function to create it, for example, one of the standard functions that belong to the Constants function category.

Target Structure Creation: A Simple Mapping Example

The figure, Target Structure Creation: A Simple Mapping Example, shows the mapping between source and target structures.

Target Structure Creation: Source Message

In the figure, Target Structure Creation: Source Message, an actual message from a sender system is used as the basis for creating target fields.

Target Structure Creation: minOccurs = maxOccurs = 1

In the figure, Target Structure Creation: minOccurs = MaxOccurs = 1, even though the MT_TargetMessage node has no target field mapping, the mapping program creates this node exactly once to match the number of occurrences specified.

Target Structure Creation: minOccurs = 0

In the figure, Target Structure Creation: minOccurs = 0, the number of occurrences of node B is between minOccurs = 0 and maxOccurs = unbounded. However, because a target field mapping is defined for node B, the mapping program creates it in the target structure as often as required, according to the source structure and target field mapping (in this case, three times).

Note

The attribute setting minOccurs > 0 for a field does not necessarily mean that the field will exist in the target message. Assuming that the field is part of a structure node with minOccurs = 0, and if no mapping is assigned to that structure, neither the structure nor the subordinate fields are created. In the example, this applies to node B and the field item.

Target Structure Creation: 0 < minOccurs < maxOccurs

In the figure, Target Structure Creation: 0 < minOccurs < maxOccurs, the number of occurrences for the target field item is between 1 and unbounded. In practice, however, this field cannot be created an infinite number of times. How often the mapping program creates this field depends on the corresponding source fields, how often they occur, and the relevant target field mapping.

Debugging View for Description Target-Field Mapping

You can use the mapping editor to debug all the steps in a message mapping by displaying the contents of the relevant queue. For each element in the data flow editor (both fields and functions), you can display the contents of the queue by choosing Display Queue from the context menu.

The message mapping in the figure, Message Mapping for the Target Field Description, shows an example of a more complex message mapping for the target field Description.

In this example, the source field LANGKEY is first compared with the constant EN. If this comparison is successful, the contents of the source field TEXT are copied to the target field Description. Based on the contents of the queue equalsS, the value [true] is only determined in the second case. This means that the content toothpaste is copied from the middle field TEXT to the target field Description. This result is represented in the queue for the IfWithoutElse element.

Debugging View for the Description Target Field Mapping

To debug message mappings, you need to display the queue. The result from each step of the message mapping is represented in such a way that you can examine the transformation from the source fields to the target fields in a step by step manner.

The displayed queue contains the following entries:

  • Initial context change

    The first gray entry represents the initial change that is not part of the contents of the queue.

  • Final context change

    The last black entry represents final context changes that are not part of the contents of the queue.

  • Suppressed entries

    The two turquoise entries are the suppressed entries; they result from the [false] entries of the if part of the IfWithoutElse standard function.

Create a Message Mapping with Context Handling

Business Scenario

You need to use queues and context when there is a need to group or perform conditional mapping and when the source and target structures are complex and not identical.

Exercise Information

Note

In this exercise, when the values include ##, replace the character with a two-digit number (01–30).

Exercise Options

You can perform this exercise in two ways:

  1. Live Environment: choose Start Exercise, and from the entry page choose Open PDF Document. Follow the steps described in this pdf in your own system landscape.
  2. Simulation: choose Start Exercise, and from the entry page choose Start Tutorial. Watch the step-by-step instructions within the simulation.

Note

We recommend running the simulation first.

Task 1: Create a Message Mapping with Context Handling

Steps

  1. Create a message mapping with the following details:

    ParameterValue
    Message mapping nameMaterialToProductDefinition
    Source message typeMATERIAL
    Target message typeProductDefinition
    Software component versionSC_BIT_MAP 1.0
    Namespacehttp://educationsap.com/pi/mapping##
    1. In the browser, enter http://nwktdc00.wdf.sap.corp:50000/dir.

    2. Go to Enterprise Services RepositoryEnterprise Services Builder.

    3. Log on with your BIT500-## user ID and password.

    4. Choose Unrestricted as the profile.

    5. In the object navigator, choose SC_BIT_MAP of educationsap.comSC_BIT_MAP 1.0 of educationsap.comhttp://educationsap.com/pi/mapping##.

    6. Choose (Create Object).

    7. In the Create Object window, navigate to Mapping ObjectsMessage Mapping. Enter the name MaterialToProductDefinition and a description of your choice.

    8. Choose the Create button.

    9. In the object navigator, choose SC_BIT_MAP of edicationsap.comSC_BIT_MAP 1.0 of educationsap.comBasis ObjectsSC_BIT_BASE_COMP 1.0 of educationsap.comurn:educationsap.com:bit:mappingMessage Types.

    10. Drag the MATERIAL message type to the text Enter a source message.

    11. Drag the ProductDefinition message type to the text Enter a target message.

  2. Transfer the source field MATNO to the target field RefNo.

    1. To create a one-to-one mapping between the source field MATNO and the target field RefNo, drag MATNO directly to RefNo.

  3. Check the source field LANGKEY for EN and copy the corresponding contents of the source field TEXT to the target field Description.

    1. Double-click the Description target field to load it into the data flow editor.

    2. Drag the TEXT and LANGKEY source fields to the data-flow editor.

    3. From the Text function category, drag the equalsS function to the data-flow editor.

    4. From the Boolean function category, drag the IfWithoutElse function to the data-flow editor.

    5. From the Constants function category, drag the Constant function to the data-flow editor.

    6. Connect the fields in the data-flow editor, as follows:

      Description = IfWithoutElse(equalsS(LANGKEY,Constant),TEXT)

    7. In the context menu of the Constant function, choose Properties.

    8. In the ‘Constant’ Parameters dialog box, enter EN, then choose OK.

    9. Save your mapping.

  4. Use the mapping editor test function to test the message mapping. Create three different entries for the MATTEXT node. Give each TEXT and LANGKEY field a unique value, and ensure that one LANGKEY field has the value EN. Display the resulting queues.

    1. On the Test tab page, choose the Generate Instance button to create a new XML instance.

      Note

      If the Tabular Tree is not showing choose the Collapse Tree button.
    2. Enter a value of your choice into the field MATNO

    3. Expand the MATERIAL node.

    4. In the context menu of MATTEXT, select Duplicate Subtree twice.

    5. In each of the MATTEXT nodes, enter a different text and language key. Use the values from the table:

      FieldnameValue
      LANGKEYEN
      TEXTBike
      LANGKEYDE
      TEXTFahrrad
      LANGKEYFR
      TEXTVelo
    6. Choose (Start the Transformation).

    7. Close the Processing Log dialog box.

    8. You will see the result in the Result area. If necessary, expand the ProductDefinition node, to see the result.

  5. Change the parameter of the Constant function to FR and test the mapping again.

    1. Check that the message mapping is in edit mode.

    2. In the context menu of the Constant function, choose Properties.

    3. In the ‘Constant’ Parameters dialog box, enter FR, then choose OK.

    4. Repeat the message mapping test.

      Result

      The test returns an error. This is because that LANGKEY and TEXT are in the wrong context.
  6. Change the context from MATTEXT to MATERIAL for nodes LANGKEY and TEXT.

    1. Switch to the Definition tab.

    2. In the data-flow editor, in the context menu of TEXT, choose ContextMATERIAL.

    3. In the context menu of LANGKEY, choose ContextMATERIAL.

    4. Test your mapping again. The result will show you the Description depending on the selected Constant value.

Log in to track your progress & complete quizzes