Tutorial: Building Your First Data Transformation

     Previous  Next    Open TOC in new window    View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Step 3: Mapping Elements and Attributes

In this step, you map source nodes to target nodes. The following figure shows the mapping of example XML data.

Figure 4-1 Mapping Example

Mapping Example

In the preceding figure, the source XML data has a different format than the target XML data. When building a query invoked by a Transformation method, you map the source nodes to target nodes as represented by the arrows. During run time, the transformation uses the mappings to convert the data from the source format to the target format. For example, the arrow labeled 1 represents the transformation of the priceQuote/customerName element to the quote/name element.

The mapping of the address data, is a more complex transformation, as represented by the arrow labeled 2 in the preceding figure. To transform the address information, all the attributes of the shipAddress element (street, city, state, and zip) must be converted to a single string XML element called address.

The source XML data is valid to a different XML Schema than the target XML data. As shown in the preceding figure, the example source XML document called PriceQuote.xml is valid to the XML Schema in the PriceQuote.xsd file. Additionally, the example source XML document called Quote.xml is valid to the XML Schema in the Quote.xsd file.

Note: The PriceQuote.xml, AvailQuote.xml, QuoteRequest.xml files are located in the Tutorial_Process_ApplicationWeb/requestquote/testxml directory of the application.
Note: Figure 4-1 shows one source data type (priceQuote). This is just one of the three sources to the myJoin method as described in Step 2: Building the Transformation. In this step, the mappings between the XML Schema in the PriceQuote.xsd file to the XML Schema in the Quote.xsd file are discussed. In the Step 4: Mapping Repeating Elements—Creating a Join, mappings between the other source types (AvailQuote.xsd and taxRate) are discussed.
Note: The PriceQuote.xml, AvailQuote.xml, QuoteRequest_a.xml, QuoteRequest.xml, and Quote.xml files are provided as examples and are not used by the business process during run time. During run time, the business process constructs the source XML data, and passes it to the transformation as described in the Introduction of this tutorial.

Complete the following tasks to create, alter, and test mappings between the source and target data:

To Map a Node From a Source to a Target

In this step, you map the XML string element called customerName from the source (PriceQuote.xsd) to the XML string element called name in target (Quote.xsd).

  1. View myJoin.xq in the Design view:
    1. In the Navigator pane, browse to the src/requestquote folder, and double click on myJoin.xq.
    2. The Design view displays the a graphical representation of the selected sources in the Source pane, as shown in the following figure.

      Figure 4-2 Graphical Representation of Design View
      Note: If the priceQuote1, availQuote1, and taxRate nodes are not displayed in your Source pane, follow the instructions in To Select the Source Types.

      The nodes displayed in the Source pane correspond to source parameters of the myJoin method of the MyTutorialJoin Transformation file. The signature of the myJoin method from the MyTutorialJoin.java file is shown in the following Java code segment:

      public abstract org.example.quote.QuoteDocument myJoin(org.example.price.PriceQuoteDocument priceQuote1, org.example.avail.AvailQuoteDocument availQuote1, float taxRate);
  2. From the Source pane of the myJoin XQ file, drag-and-drop the priceQuote1/customerName node onto the quote/name node in the Target pane.
  3. A solid line appears between the two elements. This solid line represents a data link between the two nodes—a link that converts the value of the source node directly to the value of the target.

    Figure 4-3 Link between two elements

Link between two elements

This link corresponds to the mapping represented with an arrow (labeled with the number 1) in Figure 4-1.

To Map Attributes of an Element to Single Element

In this step, you will map multiple attributes of one element to another single element.

The XML priceQuote1/shipAddress element contains the following attributes:

All these attributes will be mapped to the single XML quote/address element of type string. This mapping is represented by the arrow labeled 2 in Figure 4-1.

Link the multiple shipAddress attributes from the Source pane to the Target pane with drag-and-drop operations, as described in the following procedure:

  1. In the Source pane, select the street attribute of priceQuote1/shipAddress node.
  2. Drag-and-drop the Street attribute from the Source pane to the quote/address node in the Target pane.
  3. Similarly select each of the individual attributes, drag-and-drop them from the Source pane to the quote/address node in the Target pane.
  4. Note: Note: You can also use the Shift key to select groups of nodes.

    Four new links are displayed, as shown in the following figure.

    Figure 4-4 Create Links

Create Links

The links labeled with numbers in Figure 4-4, correspond to the mappings represented as arrows (label number 2) in Figure 4-1.

To View and Save the Generated Simple Query

A query (in the XQuery language) is generated when you create mapping links from source elements and attributes to target elements and attributes.

  1. Select the Source view of the myJoin.xq file.
  2. The generated query is displayed as shown

    Figure 4-5 Source view of the myjoin.xq file


    Source view of the myjoin.xq file

    Note: The XQuery code labeled with numbers in Figure 4-1 correspond to the numbered mappings and links in Figure 4-4, respectively.
  3. Save all the files in this application. From the BEA WorkSpace Studio menu bar, choose File > Save All. You can also save all the files by entering Ctrl+Shift+S.
  4. Note: Pressing Ctrl+S saves just the active file
To Test a Simple Query

This section describes the steps necessary to test the query generated in the preceding section. In this section, you will enter source XML data, run that data against the query, and view the resulting target XML data.

  1. Select the Test tab of myJoin.xq file.
  2. Import PriceQuote.xml as source data for the transformation:
    1. From the Source Variable drop-down menu in the Source Data pane, select priceQuote1.
    2. Click the Import icon.
    3. The Import File dialog box is displayed.

    4. Double-click the src folder.
    5. Double-click the testxml folder.
    6. Click the PriceQuote.xml file.
    7. Click Open.
    8. A graphical representation of the PriceQuote.xml file is displayed in the Source Data pane.

  3. In the Result Data pane, click the Test XQuery icon.
  4. The source XML data in one format is transformed by the query to XML in the target format is displayed in the Result Data pane, as shown in the following figure.

    Figure 4-6 Result Data Pane


    Result Data Pane

    To learn more about the transformation occurring in the query including a walk through of the generated XQuery code, see Understanding the Transformation.

    Note: In the XML document, the string: quot is the namespace prefix for the following namespace URI: xmlns:quot="http://www.example.org/quote". To learn more about namespace declarations and how this XML data was generated, see Understanding the Transformation.
To Edit and Retest the Simple Query

This section provides the steps for editing the generated query to add a delimiter between the street, city, state, and zip code fields of the address element.

  1. Select the Design tab of myJoin.xq file.
  2. Select the link between the zip attribute of the priceQuote1/shipAddress node and the quote/address node.
  3. In the General Expression pane of the Target Expression tab, add the argument: ",", between the address attribute parameters of the concat function to delineate between the different address fields, as shown in the following listing:
  4. concat($priceQuote1/ns0:shipAddress/@street,",",
    $priceQuote1/ns0:shipAddress/@city,",",
    $priceQuote1/ns0:shipAddress/@state,",",
    $priceQuote1/ns0:shipAddress/@zip)
  5. Click Apply.
  6. The updated map is displayed as shown in the following figure.

    In the proceeding step, you modified the links between shipAddress attributes and the address element in the query, which causes these links to change from direct data links (represented as blue lines) to implied links (represented as light gray lines) as show in the following figure. The mapper parses the XQuery code and determines that there are implied links between the target and source elements.

    Figure 4-7 Mapping Between Source and Target Panes


    Mapping Between Source and Target Panes

  7. Select the Test tab of myJoin.xq file and in the Result Data pane click Test.
  8. In the Result Data pane, the resulting XML data is displayed.

    The street, city, state, and zip code fields of the address element will be delineated by a commas, as shown in the following listing:

    <address>12 Springs Rd,Morris Plains,nj,07960</address>
To Add an XQuery Function Call to the Query

This section provides steps for converting the state field to uppercase by calling a standard W3C XQuery function from the query.

  1. Select the Design tab of myJoin.xq file.
  2. Switch to XQuery Transformation perspective to view the Target Expression pane. To switch to XQuery Transformation perspective, select Window > Open Perspective > XQuery Transformation from the BEA WorkSpace Studio menu.
  3. Select the link between the state attribute of the shipAddress element and the quote/address element.
  4. In the Source pane, the state attribute link gets highlighted in green.

    In the General Expression pane of the Target Expression tab, the call to the concat function is displayed.

  5. In the General Expression pane, find the following text:
  6. $priceQuote1/ns0:shipAddress/@state
    Figure 4-8 Target Expression Pane - 1
  7. From the XQuery Functions folder, in the Expressions Functions palette expand String Functions.
  8. Select the upper-case function, and drag-and-drop it over the $priceQuote1/ns0:shipAddress/@state attribute in the General Expression pane.
  9. The following is displayed in the General Expression pane, as shown in the following figure.

    Figure 4-9 Target Expression Pane - 2


    Target Expression Pane - 2

    Leave $string-var selected in the General Expression pane as shown in the preceding figure.

  10. In the Source pane, select the priceQuote1/shipAddress/state node and drag-and-drop it over the $string-var parameter of the General Expression pane.
  11. In General Expression pane the following is displayed, as shown in the following figure.

    Figure 4-10 Target Expression Pane - 3


    Target Expression Pane - 3

  12. Click Apply and save myJoin.xq file.
  13. Select the Test tab.
  14. Click the Test XQuery icon.
  15. In the XML Source tab of the Result Data pane, the state is displayed in uppercase characters, as shown in the following listing:

    <address>12 Springs Rd,Morris Plains,NJ,07960</address>

  Back to Top       Previous  Next