The following example shows adding nested if-then-else expressions to a link to supply the appropriate supplier id for an incoming purchase order. In addition this section shows how you can use functions in if-then-else expressions.
In this example, if partId is between 0 and 200, use the Supplier with the id of 12453. If the partId is between 201 and 400, the Supplier id is dependent on the quantity requested. If the quantity requested is over the maximum volume for one Supplier (id: 35667) use the high volume Supplier (id: 20045.) In addition, this example appends the source countryCode to the supplier id code using the concat function call as shown in the following figure.
To Create the If-Then-Else Example with Complex Conditions
For this example, import the files: PurchaseOrder.xsd and Supplier.xsd files (step 2 in General Steps to Open or Create a Query in the Mapper.) If you installed WebLogic Platform in the c:\bea directory, import these files from the c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\ifThenElse directory.
For this example, select and add the PurchaseOrder.xsd/PurchaseOrder node as an source type (step 4 in General Steps to Open or Create a Query in the Mapper.)
For this example, select and add the Supplier.xsd/Supplier node as an target type (step 4 in General Steps to Open or Create a Query in the Mapper.)
Keep the link selected for the next step.
An XQuery if-then-else construct is added to the link.
The If Condition pane appears.
The first condition is added to the if section of the if-then-else.
The Join Type determines how the conditions that make up if section of the if-then-else are evaluated during run time.
The second condition is added to the if section of the if-then-else.
During run time, for example if partId is equal to 100, the if condition is evaluated as described here:
The contents of the Then Expression are selected.
Keep the text selected for the next step.
The text in the Then Expression pane is deleted.
The $string-var argument is replaced by the string: "-12453".
The following XQuery code of the then expression is saved:
xf:concat($PurchaseOrderDoc/countryCode, "-12453")
During run time, when the if condition evaluates to true, the then expression is evaluated. When the then expression created in this step is evaluated, it concatenates the countryCode with supplier id of 12453 and returns this value as the Target Supplier/id.
The nested if-then-else added in this step, tests if the supplied source partId is between 201 and 400. For partIds in this range, the returned Supplier id is dependent on the quantity requested. If the quantity requested is over the maximum volume for one Supplier (id: 20045) the high volume Supplier (id: 35667) is returned.
To add the nested if-then-else to the else expression:
A nested if-then-else is added to the else and Else becomes Else If Condition.
From the Source pane, drag-and-drop the PurchaseOrderDoc/partId element into the Left Hand Expression section of the If Condition pane.
In the Right Hand Expression section of the Else If Condition pane, enter: 200 and click Add.
The first condition is added to the if section of the second level if-then-else.
From the Source pane, drag the PurchaseOrderDoc/partId element into the Left Hand Expression section of the Else If Condition pane.
In the Right Hand Expression section of the Else If Condition pane, enter: 400.
The selected Join Type determines how the conditions that make up if section of the if-then-else are evaluated during run time.
The second condition is added to the if section of the second level if-then-else.
In the Target Expression tab the following is displayed as shown in the following figure.
To add the third if-then-else to the then expression:
A nested third level if-then-else is added to the then and Then Expression becomes Then If Condition.
From the Source pane, drag the PurchaseOrderDoc/quantity element into the Left Hand Expression section of the Then If Condition pane as in the following figure.
Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)
In the Palette, expand Type Functions.
Select the decimal function and drag-and-drop it into the Right Hand Expression section of the Then If Condition pane. Leave the first $string-var argument selected for the next step.
From the Source pane, drag the PurchaseOrderDoc/maxVolumeForSupplier element over the first $string-var argument of the decimal function in the Right Hand Expression section of the Then If Condition pane and click Add.
A condition is added to the if section of the third level if-then-else.
Warning: You must explicitly cast the PurchaseOrderDoc/maxVolumeForSupplier element as a decimal to force the comparison between the Left Hand Expression and the Right Hand Expression to be a numerical comparison and not a lexicographical one.
The following is displayed in the Target Expression tab.
The $string-var argument is replaced by "-20045".
The XQuery code of the then expression of the third level if-then-else is saved.
During run time, when the if condition evaluates to true, the then expression is evaluated. If the then expression created in this step is evaluated, it concatenates the countryCode with supplier id of 20045 and returns this value as the Target Supplier/id.
The following is displayed in the Target Expression tab.
The $string-var argument is replaced by "-35667".
The XQuery code of the else expression of the third level if-then-else is saved.
During run time, when the if condition evaluates to false, the else expression is evaluated. If the else expression created in this step is evaluated, it concatenates the countryCode with supplier id of 35667 and returns this value as the Target Supplier/id.
The following is displayed in the Target Expression tab.
The $string-var argument is replaced by "-56898".
if ((data($PurchaseOrderDoc/partId) > 0
and data($PurchaseOrderDoc/partId)<= 200)) then
xf:concat($PurchaseOrderDoc/countryCode,
"-12353")
else
if ((data($PurchaseOrderDoc/partId) > 200
and data($PurchaseOrderDoc/partId) <= 400)) then
if (data($PurchaseOrderDoc/quantity) >
xs:decimal(data($PurchaseOrderDoc/maxVolumeForSupplier))) then
xf:concat($PurchaseOrderDoc/countryCode,
"-20045")
else
xf:concat($PurchaseOrderDoc/countryCode,
"-35667")
else
xf:concat($PurchaseOrderDoc/countryCode,
"-56898")
Note: You can cut and past directory paths into the Name field of the Open File to Test pane to jump to directory locations. If you installed WebLogic Platform in the c:\bea directory, you can jump to the directory that contains the XML files for this example, by pasting the following directory path into the Name field: c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\ifThenElse\XML and then pressing enter.
If not currently running, the WebLogic Server for the current application will be started. In order for a query to run, the WebLogic Server for the current application must be running.
In the Result Data pane, a graphical representation of the target data is displayed.
In the Result Data pane, the resulting value of Supplier/id element is UK-35667. The specified source partId (345) is between 200 and 400 and the specified source quantity (678) is less than the specified source maxVolumeForSupplier (1000), so the else expression: xf:concat($PurchaseOrderDoc/countryCode, "-", xf:string(35667)) is evaluated (shown in bold in the preceeding XQuery code listing), resulting in the string: UK-35667 being returned as the value of the Supplier/id element as shown in the following resulting XML data:
<?xml version="1.0" encoding="UTF-8"?> <Supplier> <id>UK-35667</id> </Supplier>
![]() |
![]() |