Samples Tutorial

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

Building XQueries in XQuery Editor View

In concrete terms, a data service is simply a file that contains XML Query (XQuery) instructions for retrieving, aggregating, and transforming data. Essentially you create a query function by:

You can also modify the Return type, either within XQuery Editor View or using an external tool.

In this lesson, you will use XQuery Editor View to develop a variety of XQuery instructions.

 


Objectives

After completing this lesson, you will be able to:

 


Overview

XQuery Editor View provides a graphical, drag-and-drop approach to constructing queries. Using XQuery Editor View, you can:

Changes that you make in XQuery Editor View are immediately reflected in Source View. Similarly, changes you make in Source View will be immediately effective in XQuery Editor View.

 


18.1 Importing Schemas for Query Development

To simplify development time in this lesson you will use ready-made schemas that define a data service's Return type.

Objectives

In this exercise, you will:

Instructions

  1. Create a new folder in the DataServices project folder, and name it MyQueries.
    1. Right-click the MyQueries folder and choose Import.
    2. Navigate to <beahome>\weblogic81\samples\LiquidData\EvalGuide\MyQueries, select the schemas folder, and click Import. This will automatically create a folder named schemas, and appropriate .xsd files, within the MyQueries directory. These .xsd files will be used to determine the Return type for all queries developed in this lesson.

 


18.2 Creating Source-to-Target Mappings

Every function within a logical data service includes source-to-target mappings that define what results will be returned by the function. As described in Part I, there are several types of mappings:

Alternatively, if you simply want to add a child element to a Return type, you can drag a source element to a complex element in your Return type. The element will be added as a child of the complex element and mapped accordingly.

Objectives

In this exercise, you will:

Instructions

  1. Right-click the MyQueries folder, choose New Arrow symbol Data Service, and use CustomerInfo.ds in the Name field.
  2. In Design View, associate the CustomerInfo data service with the CUSTOMER.xsd schema. The schema is located in MyQueries\schemas.
  3. Add a new function to the CustomerInfo data service and name it getAllCustomers.
  4. Figure 18-1 Design View of CustomerInfo Data Service


    Design View of CustomerInfo Data Service

  5. Click the getAllCustomers() function to open XQuery Editor View.
  6. Add a for node to the work area by completing the following steps:
    1. In the Data Services Palette, open the CUSTOMER.ds folder, located in DataServices\CustomerDB.
    2. Drag and drop CUSTOMER() into XQuery Editor View. This creates a For:$CUSTOMER source node.
  7. Create a simple mapping. Drag and drop each element in the CUSTOMER source node onto the corresponding element in the Return type.
  8. Note: You do not need to map the LOGIN_ID element.
    Figure 18-2 Simple Mapping


    Simple Mapping

  9. Create an induced mapping, by completing the following steps:
    1. Delete all the simple mappings. (Right-click a map line and select Delete from the pop-up menu.)
    2. Drag and drop the CUSTOMER* element (source node) onto the CUSTOMER element in the Return type.
    3. Notice that the mappings are automatically generated for each element, because the source and target element names are the same.

      Figure 18-3 Induced Mapping


      Induced Mapping

  10. Create an overwrite mapping, by completing the following steps:
    1. In the Return type right-click the CUSTOMER element and choose Add Child Element.
    2. Double-click the NewChildElement, enter Addresses, and press Enter.
    3. In the Data Services Palette, open the ADDRESS.ds icon, which is located in the DataServices\CustomerDB folder.
    4. Drag and drop ADDRESS() into XQuery Editor View.
    5. Press Ctrl, and then drag and drop ADDRESS* element (source node) onto the Addresses element in the Return type.
    6. Notice that the entire complex ADDRESS* element is brought to the target, where it overwrites the element, instead of adding it as a child.

      Figure 18-4 Overwrite Mapping


      Overwrite Mapping

 


18.3 Creating a Basic Parameterized Function

A parameterized query lets you filter returned data based on specific criteria, such as a particular order number, customer name, or customer number.

Objectives

In this exercise, you will:

Instructions

In Design View: Add a new function to the CustomerInfo data service and name it getCustomerByName.

  1. Click getCustomerByName() to open XQuery Editor View for that function.
  2. Add a for node, by completing the following steps:
    1. In the Data Services Palette, open the CUSTOMER.ds folder, which is located in the DataServices\CustomerDB folder.
    2. Drag and drop CUSTOMER() into XQuery Editor View. This creates a For:$CUSTOMER source node.
  3. Create an induced mapping. Drag and drop the CUSTOMER* element (source node) onto the CUSTOMER element in the Return type.
  4. Add a parameter, by completing the following steps:
    1. Right-click an empty spot in XQuery Editor View.
    2. Choose Add Parameter.
    3. Enter FirstName in the Parameter Name field.
    4. Select xs:string as the Primitive Type.
    5. Click OK. (You will need to move the nodes until all are visible because the new parameter node may be placed behind the CUSTOMER node.)
  5. Add a where clause, by completing the following steps:
    1. Drag and drop the parameter's string element onto FIRST_NAME element (source node). Make sure that you release the mouse button when the FIRST_NAME element is highlighted. This action creates a filter for the FIRST_NAME element based on the parameter that is passed to the function.
    2. Confirm that the where clause is correctly set by clicking the $CUSTOMER source node's header. The Expression Editor will open and you should see the following where clause:
    3. $FirstName = $CUSTOMER0/FIRST_NAME
      Figure 18-5 First Name Parameter and WHERE Clause


      First Name Parameter and WHERE Clause

  6. Add a second where clause, by completing the following steps:
    1. Add a new parameter, entering LastName, and selecting xs:string as the Primitive Type.
    2. Click the $CUSTOMER node's header. The Expression Editor opens.
    3. Triple-click inside the where field and place your cursor at the very end, after FIRST_NAME.
    4. Select the "and" logical conjunction from the pop-up operator list (the "..." icon). You can now define the where clause to filter data by last name.
    5. Note: An alternative method is to simply enter "and" in the field.
    6. Click the string element in the second parameter. The variable name $LastName appears at the end of the where clause.
    7. Choose eq: Compare Single Values from the popup operator list.
    8. Note: An alternative method is to simply enter eq in the field.
    9. Click the LAST_NAME element in the For:$CUSTOMER node. You should see the following in the where clause field:
    10. $FirstName = $CUSTOMER/FIRST_NAME and $LastName = $CUSTOMER/LAST_NAME
    11. Click the green check button to accept the changes.
    12. Figure 18-6 Query Editor View of Parameterized Query
  7. Test the function, by completing the following steps:
    1. Open CustomerInfo.ds in Test View.
    2. Select getCustomerByName(FirstName, LastName) from the drop-down list.
    3. Enter Jack in $FirstName field.
    4. Enter Black in the $LastName field.
    5. Click Execute.
    6. Confirm the results, which should be as displayed in Figure 18-7.

      Figure 18-7 Parameterized Query Results


      Parameterized Query Results

  8. Open CustomerInfo.ds in Source View to view the generated XQuery. The query should be similar to that displayed in Figure 18-8.
  9. Note: The automatic namespace assignments may not match.
    Figure 18-8 Parameterized Function Source Code


    Parameterized Function Source Code

 


18.4: Creating a String Function with a Built-In XQuery Function

The XQuery language provides more than 100 functions. BEA provides some additional, special purpose functions. In this exercise, you will build a query that uses the built-in XQuery startWith() function to create business logic sufficient to retrieve records based on an OR condition.

Objectives

In this exercise, you will:

Instructions

  1. Add a new function to the CustomerInfo data service and name it getCustomerBySSN.
  2. Click getCustomerBySSN() to open XQuery Editor View to that function.
  3. Add a for clause, by completing the following steps:
    1. In the Data Services Palette, open the CUSTOMER.ds folder, which is located in DataServices\CustomerDB.
    2. Drag and drop CUSTOMER() into XQuery Editor View. This creates a For:$CUSTOMER node.
  4. 4. Create an induced map. Drag and drop the CUSTOMER* element (source) onto the CUSTOMER element in the Return type.
  5. Add a new parameter, entering SSN as the Parameter Name, and selecting xs:string as the Primitive Type.
  6. Add a where clause that uses a built-in XQuery function, by completing the following steps:
    1. Click the $CUSTOMER node's header. The Expression Editor opens.
    2. Click the Add Where Clause icon.
    3. In XQuery Function Palette, expand the String Functions folder.
    4. Drag and drop the following function into the where clause field.
    5. fn:starts-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean
    6. Confirm that the where clause now includes the following built-in function:
    7.  fn:starts-with($arg1, $arg2) 
    8. Edit the where clause, so that it reads as follows:
    9. fn:starts-with($CUSTOMER/SSN, $SSN) 
    10. Click the green check button to accept the changes.
    11. Figure 18-9 Built-In Function Where Clause


      Built-In Function Where Clause

  7. Test the function, by completing the following steps:
    1. Open CustomerInfo.ds in Test View.
    2. Select getCustomerBySSN() from the Function drop-down list.
    3. Enter 647 in the xs:string SSN field.
    4. Click Execute.
    5. Confirm the results, which should be as displayed in Figure 18-10.
    6. Figure 18-10 Built-In Function Test Results


      Built-In Function Test Results

  8. Open CustomerInfo.ds in Source View to view the generated XQuery. The query should be similar to that displayed in Figure 18-11.
  9. Note: The automatic namespace assignments may not match.
    Figure 18-11 Source View of Built-In String Function


    Source View of Built-In String Function

 


18.5: Creating a Date Function

A date function lets you retrieve data based on date parameters.

Objectives

In this exercise, you will:

Instructions

  1. Add a new function to the CustomerInfo data service and name it getCustomerByBirthYear.
  2. Click getCustomerByBirthYear() to open XQuery Editor View to that function.
  3. Add a for clause, by completing the following steps:
    1. In the Data Services Palette, open the CUSTOMER.ds folder, which is located in DataServices\CustomerDB.
    2. Drag and drop CUSTOMER() into XQuery Editor View. This creates a for node for the CUSTOMER() function.
  4. Create an induced mapping. Drag and drop the CUSTOMER* element (source) onto the CUSTOMER element (Return).
  5. Create a new parameter, enter BirthYear as the Parameter Name, and select xs:integer as the Primitive Type.
  6. Add a where clause, by completing the following steps:
    1. Click the $CUSTOMER node's header. The Expression Editor opens.
    2. Click the Add Where Clause icon.
    3. In XQuery Function Palette, expand the Duration, Date, and Time Functions folder.
    4. Drag and drop the built-in following function into the where clause field.
    5. fn:year-from-date($arg as xs:date?) as xs:integer? 
    6. Confirm that the where clause is as follows:
    7. fn:year-from-date($arg)
    8. Edit the built-in function, so that it reads as:
    9. fn:year-from-date($CUSTOMER/BIRTH_DAY) eq $BirthYear
    10. Click the green check button to accept the changes.
    11. Figure 18-12 Where Clause Using a Built-In Date Function
  7. Test the function, by completing the following steps:
    1. Open CustomerInfo.ds in Test View.
    2. Select getCustomerByBirthYear() from the function drop-down list.
    3. Enter 1970 in the $arg0 field.
    4. Click Execute.
    5. Confirm the results, which should be as displayed in Figure 18-13. There should be five customer profiles returned.
    6. Figure 18-13 Date Function Test Results


      Date Function Test Results

  8. Open CustomerInfo.ds in Source View to view the generated XQuery. The query should be similar to that displayed in Figure 18-14.
  9. Note: The automatic namespace assignments may not match.
    Figure 18-14 Date Function Source View


    Date Function Source View

 


18.6: Creating Outer Joins and Order By Expressions

Outer joins return all records from one table even it doesn't contain values that match those in the other table. For example, an outer join of customers and orders reports all customers—even those without orders.

Objectives

In this exercise, you will:

Instructions

  1. Add a new data service to the MyQueries folder and name it CustomerAddresses.
  2. Associate the CustomerAddresses() data service with the CUSTOMERADDRESS.xsd schema. The schema is located in MyQueries\schemas.
  3. Add a new function to the CustomerAddresses data service and name it getCustomerAddresses.
  4. Figure 18-15 Design View of CustomerAddresses Data Service


    Design View of CustomerAddresses Data Service

  5. Click getCustomerAddresses() to open XQuery Editor View for that function.
  6. Add two for nodes to the work area, by completing the following steps:
    1. In the Data Services Palette, expand DataServices\CustomerDB.
    2. Open the CUSTOMER.ds folder (located in the CustomerDB folder), and then drag and drop CUSTOMER() into XQuery Editor View.
    3. Open the ADDRESS.ds folder (located in the CustomerDB folder), and then drag and drop ADDRESS() into XQuery Editor View.
    4. Figure 18-16 Source Nodes


      Source Nodes

  7. Create an induced mapping for the CUSTOMER node. Drag and drop the CUSTOMER* element (source) onto the CUSTOMER element (Return).
  8. Create an induced mapping for the ADDRESS node. Drag and drop the ADDRESS* element (source) onto the ADDRESS element (Return).
  9. Note: Do not drop the source element onto the ADDRESSES element.
  10. Create a source node relationship. Drag and drop the CUSTOMER_ID element in the $CUSTOMER node onto the corresponding element in the $ADDRESS node.
  11. Figure 18-17 Mapped and Joined Source Nodes


    Mapped and Joined Source Nodes

  12. Add an OrderBy clause, by completing the following steps:
    1. Click the ADDRESS node's header. The Expression Editor opens.
    2. Click the Order By Clause icon.
    3. Click inside the Order By Clause field.
    4. Enter $ADDRESS/ZIPCODE descending in the field.
    5. Click the green check button to accept the changes.
    6. Figure 18-18 OrderBy Clause


      OrderBy Clause

  13. Test the function, by completing the following steps:
    1. Open CustomerAddresses.ds in Test View.
    2. Select getCustomerAddresses() from the function drop-down list.
    3. Click Execute.
    4. Confirm the results. Addresses should be nested after the customer's information.
    5. Figure 18-19 Order By Test Results


      Order By Test Results

  14. Open CustomerAddresses.ds in Source View to view the generated XQuery.
  15. Note: The automatic namespace assignments may not match.
    Figure 18-20 CustomerAddresses() Source View


    CustomerAddresses() Source View

 


18.7: Creating Group By and Aggregate Expressions

Sometimes, you may want to group data according to particular data elements, such as grouping customers by state and country.

Objectives

In this exercise, you will:

Instructions

  1. Create a new data service in the MyQueries folder and name it CustomerOrders.
  2. Associate the CustomerOrders data service with the CUSTOMER_ORDER.xsd schema. The schema is located in MyQueries\schemas.
  3. Create a new function and name it getCustomerOrderAmount.
  4. Figure 18-21 Design View of Customer Orders Data Service


    Design View of Customer Orders Data Service

  5. Click getCustomerOrderAmount to open XQuery Editor View for that function.
  6. Add a for node, by completing the following steps:
    1. In the Data Services Palette, open the CUSTOMER_ORDER.ds folder, which is located in DataServices\ApparelDB.
    2. Drag and drop CUSTOMER_ORDER() into XQuery Editor View.
  7. Create a GroupBy clause, by completing the following steps:
    1. Right-click the C_ID element in the $CUSTOMER_ORDER source node.
    2. Choose Create Group By. A GroupBy node is created.
  8. Create a simple mapping. Drag and drop the TOTAL_ORDER_AMT from the Group section of the GroupBy node onto the corresponding element in the Return type.
  9. Create a simple mapping. Drag and drop the C_ID element in the By section of the GroupBy node to the corresponding element in the Return type.
  10. Figure 18-22 GroupBy Node Added and Mapped


    GroupBy Node Added and Mapped

    Modify a Return expression, by completing the following steps:

    1. Click the TOTAL_ORDER_AMOUNT, located in the Return node. The Expression Editor opens. Every element in a Return type has an underlying expression. In this case the expression is:
    2. {fn:data($CUSTOMER_ORDER_group/TOTAL_ORDER_AMT)}
    3. Edit the expression so that it changes fn:data() to fn:sum(), as follows:
    4. {fn:sum($CUSTOMER_ORDER_group/TOTAL_ORDER_AMT)}
    5. Click the green check button to accept the changes.
    6. Figure 18-23 Aggregate Expression


      Aggregate Expression

  11. Test the function, by completing the following steps:
    1. Open CustomerOrders.ds in Test View.
    2. Select getCustomerOrderAmount() from the Function drop-down list.
    3. Click Execute.
    4. Confirm the results.
    5. Figure 18-24 Aggregate Test Results


      Aggregate Test Results

  12. Open CustomerOrders.ds in Source View to view the generated XQuery.
  13. Note: The automatic namespace assignments may not match that shown in the exercise.
    Figure 18-25 Source View of the CustomerOrders Data Service


    Source View of the CustomerOrders Data Service

 


18.8: Creating Constant Expressions

Creating a data service query that uses a constant expression enables a quick and easy way to locate specific information. For example, you can use a constant expression to identify all customers who ship by Ground method.

Objectives

In this exercise, you will:

Instructions

  1. Add a new function to the CustomerInfo data service and name it getGroundCustomers.
  2. Click the getGroundCustomers() function to open the XQuery Editor View.
  3. Add a for node, by completing the following steps:
    1. In the Data Services Palette, open the CUSTOMER.ds folder, which is located in the DataServices\CustomerDB folder.
    2. Drag and drop CUSTOMER() into XQuery Editor View.
  4. Create an induced mapping. Drag and drop the entire CUSTOMER* element (source node) onto the CUSTOMER element (Return).
  5. Add a where clause, by completing the following steps:
    1. Click the CUSTOMER node's header. The Expression Editor opens.
    2. Click the Add Where Clause icon.
    3. Enter the following expression as a where clause:
    4. $CUSTOMER/DEFAULT_SHIP_METHOD eq "GROUND"
    5. Click the green check mark icon to accept the where clause for the customer object.
    6. Figure 18-26 Constant Function with Default Expression


      Constant Function with Default Expression

  6. Test the function. The results should be as displayed in Figure 18-27.
  7. Figure 18-27 Test Results of a Constant Expression


    		Test Results of a Constant Expression

  8. Open CustomerInfo.ds in Source View. The code should be as displayed in Figure 18-28.
  9. Figure 18-28 Source Code


    Source Code

 


Lesson Summary

In this lesson you learned how to:

Use the XQuery Function Palette to add built-in XQuery functions to a query.


  Back to Top       Previous  Next