Using XQuery in Oracle XML DB

This tutorial describes how to use XQuery in Oracle XML DB.

Approximately 1hour.

Topics

This tutorial covers the following topics:

Reviewing the XML Schema in Enterprise Manager
Reviewing the XML Schema in JDeveloper
Running XQuery Expressions to Access the XML Documents
Improving Performance of XQuery Expressions Through Index Creation
Using Views with XQuery
Using Virtual XML Documents
Retrieving Information from the XML Repository Using XQuery on Resource Views
Transforming an XML Document Using XQuery
Using JDBC to Query Oracle XML DB with XQuery

Place your mouse on this icon to load and view all the screenshots for this tutorial. (Caution: Because this action loads all screenshots simultaneously, response time may be slow depending on your Internet connection.)

Note: Alternatively, you can place the cursor over each individual icon in the following steps to load and view only the screenshot associated with that step.

Overview

What is XQuery?

XML Query (XQuery) is a standards effort undertaken by the World Wide Web consortium to enable data to be extracted from XML documents. XML Query is designed to work with the XML data model, and be a comprehensive query language for data that is expressed in XML, just as SQL has been the query language for much of the world’s structured data expressed as relational tables, and as keyword searches have powered much of the information access on the Internet.

Oracle Database-Native XQuery

As XQuery nears finalization, the IT community has started investigating the business uses of XML and determining what value XQuery might provide. As the innovation leader in commercial database technology, Oracle Database 10g Release 2 provides a full-featured native XQuery engine integrated with the traditional Oracle database server to help organizations explore their XQuery needs.

On the SQL side, the XML datatype was introduced in SQL 2003 as a way to encapsulate XML in SQL. The SQL committee is now working to integrate the querying of XML using XQuery. This is being accomplished by introducing a new SQL function: XMLQuery, and a new construct: XMLTable both of which operate on XML and SQL values using XQuery. The former is known as XQuery-centric approach as it allows querying and constructing XML using XQuery. The latter is known as SQL-centric approach as it allows breaking apart the XQuery values into relational values.

Oracle Database 10g Release2 enables XQuery support in the database server through these SQL standard functions. A new XQUERY command has also been implemented in SQL*Plus to allow users to enter XQuery expressions on the command line. With standards-based implementation of XQuery in Oracle Database 10g Release 2, application developers can use their favorite APIs (e.g., JDBC, ODP.NET) to access Oracle Database XQuery capabilities.

Benefits of Oracle XQuery

Using SQL/XML XQuery functions along with structured storage of XML documents, XML DB can handle XML queries with orders of magnitude performance improvement over DOM-based functional evaluation of XML queries. Furthermore, XML queries can be seamlessly merged with SQL relational queries to handle all query scenarios. Finally, the XML query capabilities of Oracle XML DB are built on the solid foundation of industry’s best relational database that is highly reliable, available, scalable, and secure. In short, the XML DB query capabilities in Oracle Database 10g Release 2 provide the most comprehensive and efficient functionality for versatile, scalable, concurrent, and high performance XML applications.

Back to Topic List

Before you perform this tutorial, you should:

1.

Perform the Installing Oracle Database 10g on Windows tutorial.

2.

Download JDeveloper 10g from OTN and unzip into a directory (c:\jdev1012).

3.

Download and unzip the xquery.zip file into your working directory (c:\wkdir).

4.

Make sure your <database oracle home>\jdk\bin directory is set in the Path environment variable settings.

Back to Topic List

Reviewing the XML Schema in Enterprise Manager

An XML Schema has been supplied as part of the OE Schema in the Sample Schema that is provided with Oracle Database 10g. In this section, you review its contents using Enterprise Manager. Perform the following steps:

1.

Open your browser and enter the following URL:

http://localhost:1158/em/

Enter system/oracle and click Login.

 

2.

Click the Administration tab.

 

3.

Scroll down.

 

4.

Click the Configuration link under XML Database.

 

5.

Enter 2100 for FTP Port and 8080 for HTTP port. Then click OK.

 

6.

Your configuration has been set. Click your Database Instance breadcrumb.

 

7.

From the Administration tab, under XML Database, select the Resources link.

 

8.

Click Go to see all the resources.

 

9.

All the resources are displayed. Expand home.

 

10.

Expand OE - PurchaseOrders - 2002 - Apr to show the list of XML documents. Click the first XML document in the list.

 

11.

General information about the document is shown. To see the actual contents of the document, click Display Contents.

 

12.

The file contents are shown. Click Show formatted XML Content.

 

13.

Review the formatted XML document. When done, close the window.

 

14.

Click the Database Instance breadcrumb.

 

15.

From the Administration tab under XML Database, click XMLType Tables.

 

16.

Click Go.

 

17.

Click the table name PURCHASEORDER.

 

18.

The definition of the XSD is displayed. Scroll down to see more information.

 

Back to Topic List

Reviewing the XML Schema in JDeveloper

To review the XML Schema in JDeveloper, you need perform the following tasks:

Back to Topic List

Create a WebDAV Connection

Before you access the XML Schema documents in XML DB, you need to create a WebDAV connection. Perform the following steps:

1.

Double-click the <jdev_dir>\jdev\bin\jdevw.exe.

Note: You may be asked if you want to upgrade from a previous release.

 

2.

Click the Connection tab. Right-click WebDAV Server in the Navigator window and select New WebDAV Connection...

 

3.

At the Welcome window, click Next.

 

4.

Enter WebDAVConnection1 in the Name field, http://localhost:8080/ in the URL field and click Authentication Required. Then click Next.

 

5.

Enter OE for the user name and password. Then click Next.

 

6.

Click Test Connection.

 

7.

Your test was successful. Click Next.

 

8.

Click Finish.

 

9.

Expand your WebDAVConnection1 connection that you just created.

 

Back to Topic

Review the XML Schema

Now you can review the XML Schema . Perform the following steps:

1.

Expand home - OE - PurchaseOrders - 2002 - Apr. Double-click the first document in the list. The XML document is shown.

 

2.

Scroll down in the Navigator window and double-click purchaseOrder.xsd.

 

3.

The XML Schema is shown in a graphical layout. Expand the PurchaseOrder type.

 

4.

Expand the Actions type.

 

5.

Expand Action and you see that there are two type objects, User and Date. Now you go back to the XML Document to see where this is defined. Click the XML Document tab.

 

6.

The Action object contains a user type of KPARTNER.

 

Back to Topic

Running XQuery Expressions to Access the XML Documents

In this section, you run some XQuery expressions and their associated explain plan to access information about your XML Documents. Perform the following steps:

1.

Open a command prompt and enter the following commands:

cd wkdir
sqlplus oe/oe

 

2.

The first xquery you run selects the number of XML documents there are in total. Execute the following script:

@xquery01

 

3.

The second xquery you run selects the number of XML documents for a particular user. Execute the following script:

@xquery02

 

4.

The next xquery you run reviews a specific purchase order. Execute the following script:

@xquery03

 

5.

The next xquery you run lists all the purchase orders who have a particular part id. Execute the following script:

@xquery04

 

6.

The next xquery you run lists the description for each line item on a particular purchase order. Execute the following script:

@xquery05


Back to Topic List

Improving Performance of XQuery Expressions Through Index Creation

You can increase the performance of your XQuery by creating an index. In this section, you create an index and then run the same XQueries to see that the explain plan has changed an the performance has improved. Perform the following steps:

1.

From your SQLPlus session, logged in the OE user.

@createXMLIndexes


2.

The next xquery you run reviews a specific purchase order. Execute the following script:

@xquery03

 

3.

The next xquery you run lists all the purchase orders who have a particular part id. Execute the following script:

@xquery04


 

4.

The next xquery you run lists the description for each line item on a particular purchase order. Execute the following script:

@xquery05


Back to Topic List

Using Views with XQuery

You can use XQuery to generate XMl from SQL data using Views. Perform the following steps:

1.

You first will create an XML view over relational tables by using a XMLTable() SQL/XML function and an XQuery expression. Execute the following script:

@createXQueryView


2.

Now you can show XQuery over the XML view you just created. Execute the following script:

@xqueryXQLView

 

Back to Topic List

Using Virtual XML Documents

Virtual XML Documents show how the contents of a row in an XMLType view can be exposed as a document in the Oracle XML DB repository. Perform the following tasks:

Back to Topic List

Create a Trigger

1.

You first need to create a trigger that will create a virtual XML document from the previously created XML view. Execute the following script:

@folderXQDepartments



Back to Topic

Review the Virtual XML Document in JDeveloper

1.

Switch to JDeveloper. From the Navigator window, right click /home/OE from your WebDAV connection and select Refresh.

 

2.

Expand XQDepartments.

Note: If you receive an error at this step, try performing step 1 one more time and then expand XQDepartments again.

 

3.

Double-click the first .xml document in the list. This Accounting.xml document displays XML data directly from a row in the XMLType view.

 

Back to Topic

Review the Virtual XML Document in SQL*Plus

1.

You can also query the virtual XML documents. Note the usage of XQuery standard doc() function to retrieve an XML document from the XML DB repository. First, you review the list of virtual documents that have a displayname of Accounting. Execute the following script:

@departmentsXQueries01


 

2.

Now you can display the contents of one of the virtual XML documents. Execute the following script:

@departmentsXQueries02


 

3.

And lastly, you can perform an XQuery to select one of the entries in the virtual XML documents. Execute the following script:

@departmentsXQueries03


 

Back to Topic

The contents of the XML DB repository are exposed via the RESOURCE_VIEW and PATH_VIEW.  Public synonyms make these views available to all database users. These views can be used just like any other database view. Perform the following steps:

1.

Execute the following script:

@resourceViewXQuery01

 

Back to Topic List

XQuery is a versatile query language. It can be used to transform XML documents from one structure to another. Perform the following steps:

1.

The tags and other XQuery expressions have transformed purchaseorders to generate a report of a very different XML document. Execute the following script:

@xqueryTransform


Back to Topic List

JDBC supports the XQuery language through a native implementation of SQL/XML functions: XMLQuery and XMLTable. The following examples demonstrate how to bind dynamic variables to an XQuery expression using the PASSING clause of the XMLTable() function. Perform the following tasks:

Back to Topic List

Add Your Application

1.

Switch to JDeveloper. Click the Applications tab in the Navigator and click the Add to Applications icon.


2.

Select XQuerySamples.jws from your <working directory>\JDBC directory. Then click Open.


Back to Topic

Modify Your JDBC Connection Information

1.

Expand XQuerySamples > SimpleXQuery > Application Sources, then right-click SimpleXQuery.java and click Open.


2.

Scroll down a bit to the OracleConnection statement and change the hostname and SID information, if needed.


3.

Change the JDBC connection information for all the .java files under XQueryBind.


Back to Topic

Add Your Classpath

1.

You need to set your classpath. Select Tools > Default Project Properties.


2.

Select Libraries then click New....


3.

Enter Oracle Database 10gR2 for the Library Name and click Edit for the Classpath.


4.

Click Add Entry...


5.

Navigate to the C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar file and click Select.


6.

Click Add Entry...


7.

Navigate to the C:\oracle\product\10.2.0\db_1\RDBMS\jlib\xdb.jar file and click Select.


8.

Click Add Entry...


9.

Navigate to the C:\oracle\product\10.2.0\db_1\lib\xmlparserv2.jar file and click Select.


10.

Click OK.


11.

Click OK to save your new Library.


12.

Click OK to close the Project Properties window.


13.

Now you need to add the Library to your projects. Right-click SimpleXQuery and select Project Properties.


14.

Select Oracle Database 10gR2 from the list of Libraries and click > to select it. Then click OK.


15.

Right-click XQueryBind and select Project Properties.


16.

Select Oracle Database 10gR2 from the list of Libraries and click > to select it. Then click OK.


Back to Topic

Build Your Application

1.

Right-click XQuerySamples and select Make.


2.

Review the Message window to make sure you receive no errors.


Back to Topic

Run Your Application

1.

Right-click SimpleXQuery and select Run.


2.

The output is displayed in the Log area.

Note: If you receive an error, check your path environment variable to make sure the jdk directory is specified. See Prerequisite #4.


3.

Right-click XQueryBind and select Run.


4.

The output is displayed in the Log area.


Back to Topic

Summary

In this lesson, you learned how to:

Use XQuery To Query XML Documents Stored In The Oracle XML DB

Add An XML Index And Show How The Explain Plan Changes When An XQuery Is Executed

Create An XML View With XQuery And Use XQuery To Query The XML View

Create Virtual XML Documents from An XML View And Use XQuery To Query These XML Documents

Retrieve Information From The XML Repository Using XQuery On Resource Views

Transform An XML Document Using XQuery

Use JDBC To Query Oracle XML DB With XQuery

Back to Topic List

Place the cursor over this icon to hide all screenshots.