Creating a Transformation Between a Non-Repeating Source Element and Repeating Target Group

This example shows how to map a XML Schema simple type (for example: a non-repeating single element) to a XML Schema complex type (for example: a repeating element) as shown in the following figure. In this example, you will create a transformation that during run time will take a single source element and maps it to repeating target element as shown in the following figure.

Figure : Non-Repeating Source Element to Repeating Target Group

image

To Create and Run a Transformation Between a Non-Repeating Source Element and a Repeating Target Group

  1. Create a Transformation file and a method in the Transformation file. For instructions, see Creating a Transformation File and a Transformation Method.
  2. Import the XSD file that contains the XML Schema for the source type of the transformation. For instructions, see Selecting Source and Target Types.
  3. For this example, import the file: PODate.xsd. 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\singleToRepeat directory.

    Note: The PODate.xsd file used in this example is identical to the PODate.xsd file used in Creating a Transformation Between a Repeating Source Group and Non-Repeating Target Element. If you have already imported the PODate.xsd file for the Creating a Transformation Between a Repeating Source Group and Non-Repeating Target Element example, you do not need to import it again.

  4. Import the XSD file that contains the XML Schema for the target type of the transformation. For instructions, see Selecting Source and Target Types.
  5. For this example, import the file: Dates.xsd. 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\singleToRepeat directory.

    Note: The Dates.xsd file used in this example is identical to the Dates.xsd file used in Creating a Transformation Between a Repeating Source Group and Non-Repeating Target Element. If you have already imported the Dates.xsd file for the Creating a Transformation Between a Repeating Source Group and Non-Repeating Target Element example, you do not need to import it again.

    Importing schemas files triggers a build of the current Schemas project folder. Wait until the current Schemas folder is built before proceeding to the next step. (The representations of the schemas will not be available in Available Source Types and Available Target Type panes until build is complete.)

  6. Select the Transformation method from a Transformation file.
  7. To select an existing method, in the Design View of the Transformation file:

    1. Right-click the arrow representing the method.
    2. From the drop-down menu, select Configure XQuery Transformation Method.
  8. Select the source type for the transformation:
    1. In the Available Source Types pane, expand the PODate.xsd folder.
    2. In the Available Source Types pane, select the PODate.xsd/PODate element.
    3. Click Add.

    The elements and attributes that make up the PODate.xsd/PODate element are displayed in the Selected Source Types pane.

  9. Select the target type for the transformation:
    1. In the Available Target Types pane, expand the Dates.xsd folder.
    2. In the Available Target Types pane, select the desired element.
    3. For this example, select the Dates.xsd/date element.

    4. Click Add.
    5. The elements and attributes that make up the Dates.xsd/date element are displayed in the Selected Target Types pane.

  10. Click Create Transformation.
  11. The Design View of the XQ file is displayed.

  12. In the Source pane, select the element (PODateDoc/billing-date) and drag it the repeating element (dates/date) in the Target pane.
  13. A dashed line linking the two elements is displayed. The dashed line with short dashes represents a structural link—a link that does not map data directly. The dashed-line representation for a structural link is shown in the following figure.

    image

    To learn more about links, see Link Representations.

  14. In the Source pane, select the element (PODateDoc/delivery-date) and drag it the repeating element (dates/date) in the Target pane.
  15. A dashed line (representing a structural link) between two elements is displayed and the two dates (PODateDoc/delivery-date and PODateDoc/billing-date) are automatically unioned together. In addition, the Structural Link variable PODate is automatically created and displayed in the Data Palette.

    The following XQuery is code is generated:

    <ns1:dates>
    {
    	for $PODate in $PODateDoc/ns0:billing-date union $PODateDoc/ns0:delivery-date
    	return
    		<ns1:date/>
    }
    </ns1:dates> 
    

    $PODateDoc is the source or input variable to the transformation, while $PODate is the iteration variable for union of the elements: $PODateDoc/ns0:billing-date and $PODateDoc/ns0:delivery-date.

    During run time, the for loop in the preceding XQuery code is executed twice. The first time the for loop is run, the iteration variable $PODate is equal to the first element in the union: $PODateDoc/ns0:billing-date and the second time the for loop is run the iteration variable $PODate is equal to the second element in the union: $PODateDoc/ns0:delivery-date. The XML data returned by the preceding query returns two empty elements: <ns1:date/>. The following steps will add the XQuery code to return the billing and delivery dates to the query.

  16. In the Source pane, select the PODateDoc/billing-date element and drag it the dates/value element in the Target pane.
  17. Two data links are created as shown in the following figure.

    image

    Since the structural links (PODateDoc/billing-date to dates/date) and (PODateDoc/delivery-date dates/date) are unioned together, when you created the link from the PODateDoc/billing-date element to the dates/value element (dates/value), a second data link between the PODateDoc/deilivery-date element and dates/value element is automatically created.

  18. In the Source pane, select the PODateDoc/billing-date element and drag it the dates/type element in the Target pane.
  19. Two data links are created.

    Keep the PODateDoc/billing-date to dates/type link selected for the next step.

  20. 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.)
  21. Select the If Then Else option.
  22. The XQuery if-then-else construct is added to the link. For example, the following XQuery source code segment for the link is replaced:

    data($PODate) 
    

    By the following XQuery source code segment for the link:

    if (xf:boolean("true")) then
    	data($PODate)
    else
    	() 
    
  23. Click If Condition.
  24. The If Condition pane appears.

  25. In this step, you add a condition to the if section of the if-then-else:
    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 Node Functions.
    3. Select the local-name function and drag it into the Left Hand Expression pane of the If Condition pane. Leave the $node-var argument selected.
    4. Select the Data Palette. (If the Data Palette is not visible in WebLogic Workshop, choose View —> Windows —> Data Palette from the menu bar.)
    5. From the Data Palette pane, drag-and-drop the PODate Structural Link variable over the $node-var argument of the local-name function in the If Condition pane.
    6. Select the operator: =.
    7. In the Right Hand Expression section of the If Condition pane, enter: "billing-date" and click Add.
    8. Note: You must enter double quotes around the "billing-date" string.

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

  26. Click Then Expression.
  27. In the Then Expression section, replace the existing text with the string: "BILLING".
  28. Replace the following text:

    data($PODate) 
    

    With the following string:

    "BILLING" 
    

    Note: You must enter double quotes around the "BILLING" string.

  29. Click Apply.
  30. Click Else Expression.
  31. In the Else Expression section, enter the string: "DELIVERY".
  32. Note: You must enter double quotes around the "DELIVERY" string.

  33. Click Apply.
  34. Select the General option, to view the generated if-then-else XQuery source code.
  35. The following XQuery code is displayed:

    if (xf:local-name($PODate) = "billing-date") then
    	"BILLING"
    else
    	"DELIVERY" 
    

    During run time, this if-then-else statement is executed twice by the XQuery for loop. The if part of the if-then-else executes the following condition:

    xf:local-name($PODate) = "billing-date" 
    

    In the left-hand expression of the if condition, the iteration variable $PODate is passed into the local-name function as a parameter. The local-name function returns the name of the XML element. For example: if the $PODate iteration variable contains the XML element named billing-date, the string: billing-date is returned by the local-name function and therefore, the left-hand expression of the if condition evaluates to the string: billing-date.

    The right-hand expression of the if condition is equal to the string: "billing-date".

    In the if condition, the left-hand expression is compared with the right-hand expression, and if the two strings are equal, the if condition evaluates to true and the string: "BILLING" is returned as the value of the target node: dates/date/type.

    Note: For simplicity, in this example it is assumed that if $PODate does not equal "billing-date", it must equal "delivery-date".

  36. Select the Test View tab.
  37. Import XML files as source data for the transformation.
  38. For this example, in the Source Data pane, select the $datesDoc node and import the file: PODateInput.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\singleToRepeat\XML directory.

    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\singleToRepeat\XML and then pressing enter.

  39. In the Result Data pane, click Test.
  40. 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.

  41. If the resulting data is XML data, in the Result Data pane you can view the resulting data as an XML document by selecting the XML Source View tab. The XML result for this example is also displayed in Figure: Non-Repeating Source Element to Repeating Target Group.
  42. If desired, you can validate the result data against the associated schema. In the Result Data pane of the Test View, click Validate.
  43. Save the DTF and the XQ file. From the menu bar, choose File —> Save All.
Previous Document Next Document