Adding Nested If-Then-Else Expressions with Complex Conditions to a Link

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.

Figure : Complex If-Then-Else

image

To Create the If-Then-Else Example with Complex Conditions

  1. Open a query in the mapper pane. For instructions see General Steps to Open or Create a Query in the Mapper.
  2. 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.)

  3. In the Design View, create a link by dragging-and-dropping the PurchaseOrderDoc/countryCode Source element over the Supplier/id Target node.
  4. Keep the link selected for the next step.

  5. Select the Target Expression tab. (If the Target Expression tab is not visible in WebLogic Workshop, choose View —> Windows —> Target Expression from the menu bar.)
  6. Select the If Then Else option.
  7. An XQuery if-then-else construct is added to the link.

  8. Click If Condition.
  9. The If Condition pane appears.

  10. Add the first condition to the if section of the if-then-else:
    1. From the Source pane, drag the PurchaseOrderDoc/partId element into the Left Hand Expression section of the If Condition pane.
    2. Select the operator: >.
    3. In the Right Hand Expression section of the If Condition pane, enter: 0 and click Add.

    The first condition is added to the if section of the if-then-else.

  11. Construct the second condition to the if section of the if-then-else:
    1. From the Source pane, drag-and-drop the PurchaseOrderDoc/partId element into the Left Hand Expression section of the If Condition pane.
    2. Select the operator: <=.
    3. In the Right Hand Expression section of the If Condition pane enter: 200.
  12. From the Join Type field select the AND option.
  13. The Join Type determines how the conditions that make up if section of the if-then-else are evaluated during run time.

  14. Click Add.
  15. 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:

  16. Click Then Expression.
  17. The contents of the Then Expression are selected.

    Keep the text selected for the next step.

  18. Click the Delete key.
  19. The text in the Then Expression pane is deleted.

  20. Create the then expression:
    1. Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)
    2. In the Palette, expand String Functions.
    3. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
    4. From the Source pane, drag-and-drop the PurchaseOrderDoc/countryCode element over the first $string-var argument of the concat function in the Then Expression pane.
    5. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
    6. Enter: "-12453".
    7. The $string-var argument is replaced by the string: "-12453".

    8. Click Apply.

    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.

  21. In this step, you add a nested if-then-else to the else.
  22. 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:

    1. Select the down arrow to the right of Else Expression, and from the drop-down menu select Insert Nested If-Then-Else.
    2. A nested if-then-else is added to the else and Else becomes Else If Condition.

    3. Add the first condition to the if section of the second level if-then-else:
    4. From the Source pane, drag-and-drop the PurchaseOrderDoc/partId element into the Left Hand Expression section of the If Condition pane.

      Select the operator: >.

      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.

    5. Construct the second condition to the if section of the second level if-then-else:
    6. From the Source pane, drag the PurchaseOrderDoc/partId element into the Left Hand Expression section of the Else If Condition pane.

      Select the operator: <=.

      In the Right Hand Expression section of the Else If Condition pane, enter: 400.

    7. From the Join Type field, select the AND option.
    8. The selected Join Type determines how the conditions that make up if section of the if-then-else are evaluated during run time.

    9. Click Add.

    The second condition is added to the if section of the second level if-then-else.

  23. Select Else If Condition of the second level if-then-else to collapse the Else If Condition pane.
  24. In the Target Expression tab the following is displayed as shown in the following figure.

    image

  25. In this step, you add another nested if-then-else to the then of the second level if-then-else. This if-then-else determines if the quantity requested is over the maximum volume for one Supplier (id: 20045) return the high volume Supplier (id: 35667).
  26. To add the third if-then-else to the then expression:

    1. Select the down arrow to the right of the second Then Expression at the bottom of the pane, and from the drop-down menu select Insert Nested If-Then-Else as shown in the following figure.
    2. image

      A nested third level if-then-else is added to the then and Then Expression becomes Then If Condition.

    3. Add a condition to the if section of the third level if-then-else:
    4. 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.

      image

      Select the operator: >.

      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.

  27. Create the then expression of the third level if-then-else:
    1. Select the Then Expression of the third level if-then-else.
    2. The following is displayed in the Target Expression tab.

      image

    3. Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)
    4. In the Palette, expand String Functions.
    5. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
    6. From the Source pane, drag-and-drop the PurchaseOrderDoc/countryCode element over the first $string-var argument of the concat function in the Then Expression pane.
    7. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
    8. Enter: "-20045".
    9. The $string-var argument is replaced by "-20045".

    10. Click Apply.

    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.

  28. Create the else expression of the third level if-then-else:
    1. Select the Else Expression of the third level if-then-else.
    2. The following is displayed in the Target Expression tab.

      image

    3. Select the Palette. (If the Palette is not visible in WebLogic Workshop, choose View —> Windows —> Palette from the menu bar.)
    4. In the Palette, expand String Functions.
    5. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
    6. From the Source pane, drag-and-drop the PurchaseOrderDoc/countryCode element over the first $string-var argument of the concat function in the Then Expression pane.
    7. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
    8. Enter: "-35667".
    9. The $string-var argument is replaced by "-35667".

    10. Click Apply.

    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.

  29. Create the else expression of the second level if-then-else:
    1. Select the Else Expression of the second level if-then-else.
    2. The following is displayed in the Target Expression tab.

      image

    3. In the Palette, expand String Functions.
    4. Select the concat function and drag it into the Then Expression pane. Leave the first $string-var argument selected for the next step.
    5. From the Source pane, drag-and-drop the Purchase Order Doc/contracted element over the first $string-var argument of the concat function in the Then Expression pane.
    6. Select the text: $string-var and click the Delete key. Leave the cursor in the current location.
    7. Enter: "-56898".
    8. The $string-var argument is replaced by "-56898".

    9. Click Apply.
  30. Select the General option, to view the generated XQuery source code. The following XQuery code is displayed:
  31. 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") 
    
  32. To run the query, select the Test View of the XQ file.
  33. Import the file: PurchaseOrder.xml. If you installed WebLogic Platform in the c:\bea directory, import this file from the c:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\ifThenElse\XML directory.
  34. 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.

  35. In the Result Data pane, click Test.
  36. 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> 
    
  37. Save the DTF and the XQ file. From the menu bar, choose File —> Save All.
Previous Document Next Document