Using XQuery Full Text to Search XML Content
Overview
- Create a simple XMLType table and populate it with 10,000 XML documents, and then view the table in SQL Developer.
- Use XQuery and SQL/XML to query XML documents stored in the PURCHASEORDER table in the XDBOE schema.
- Create an XML-aware full-text index.
- Perform XQuery full-text searches on the PURCHASEORDER table.
- Oracle Database 12c Release 1 (12.1) Enterprise Edition for linux.
- Oracle SQL Developer version 3.2 or higher (optional, you can use SQL*Plus in a terminal window).
- Mozilla Firefox Web browser.
- A linux text editor such as gedit.
- Install Oracle Database 12c Release 1 (12.1) Enterprise Edition for linux.
- Have access to or have installed the sample schemas.
- Install Oracle SQL Developer (optional, you can use SQL*Plus in a terminal window).
- Download the DatabaseTrack.zip file which contains the script files used in this tutorial to your local drive. In this tutorial, we copied the downloaded and extracted the required DatabaseTrack.zip file to the /home/oracle/DatabaseTrack folder on a linux machine.
- Download the xdboe_privileges.sql script which creates the new XDBOE user and grants this user some additional privileges.
- The code examples in this tutorial use a new XDBOE schema that will be created with the setup scripts.
Purpose
In this tutorial, you create a simple XMLType table named PURCHASEORDER using the default settings, and then load 10,000 XML documents into the PURCHASEORDER table. You also use XQuery Full Text to search XML documents. In order to use XQuery Full Text, you must create an XML-aware full-text index on the documents that you want to search.
Time to Complete
Approximately 60 minutes.
Introduction
This tutorial shows you how to create an XML-aware full-text index in order to search XML content by using XQuery full-text search. It covers the following topics:
Scenario
XQuery Full Text is an extension to W3C XQuery standard that makes it possible to perform complex full-text style search operations on the content of XML documents. Support for XQuery Full Text is available in Oracle Database 12.1.0.1.0 and later. The original XQuery language included an operator called CONTAINS that allow pattern-matching-based searches on XML content. The CONTAINS operator provides a case-sensitive substring-style search capability; that is, it returns TRUE if the source string contains exactly the set of characters contained in the target string. The XQuery Full Text specification adds the ability to perform word-based searches of XML content, with all of the common features of a text-retrieval system. XQuery Full Text includes support for word match, windowing (word must appear within n words of word), and stemming (automatically recognize related words). In order to provide an efficient implementation of XQuery Full Text, Oracle Database 12c makes use of Oracle’s Text indexing technology. In order to make use of XQuery Full Text it is necessary to create an XML-aware full-text index on the documents that are to be searched.
Hardware and Software Requirements
The following is a list of hardware and software requirements:
Prerequisites
Before starting this tutorial, you should:
Initialize the Hands-On Lab
Note: In this OBE, we are using HTTP on a standard port (80) instead of an Oracle XML DB Default port. You can use the Oracle XML DB configuration file, /xdbconfig.xml, to configure HTTP(S) to listen on any port. By default, HTTP(S) listens on a non-standard, unprotected port. To use HTTP or HTTPS on a standard port (80 for HTTP, 443 for HTTPS), see the Using HTTP(S) on a Standard Port Instead of an Oracle XML DB Default Port section (chapter 28) in the Oracle XML DB Developer's Guide 12c Release 1 (12.1) reference guide.
Setting the HTTP Port and Configuring the Listener
Run the Reset Script
Use SQL Developer to Establish a Connection to the XDBOE Schema
Set the Autotrace Parameters
Using XQuery in Oracle Database 12c Release 1 (12.1)
In this section, you create a simple XMLType table named PURCHASEORDER using the default settings in the XDBOE schema. You then load 10,000 XML documents into the new PURCHASEORDER table. Finally, you use XQuery and SQL/XML to query XML documents stored in the PURCHASEORDER table.
Create a Simple XMLType Table
Review the PURCHASEORDER Table in SQL Developer
Accessing XML Content Using XQuery
XQuery is to XML content what SQL is to relational data. The XQuery standard was developed by the W3C and as such, it is the natural language for querying, manipulating, and updating XML content. The following section will show how to use XQuery to work with XML content stored in an Oracle database.
The complete code for the 1.1 simpleQueries.sql script is as follows:
set long 4096
--
-- 1. Use XQuery and fn:collection to count the documents in the PURCHASEORDER
-- table.
--
select *
from XMLTABLE(
'count(fn:collection("oradb:/XDBOE/PURCHASEORDER"))'
)
/
--
-- 2. Use XQuery to select a single document from the PURCHASEORDER table
--
select *
from XMLTABLE(
'for $i in fn:collection("oradb:/XDBOE/PURCHASEORDER")/PurchaseOrder[Reference/text()=$REFERENCE]
return $i'
passing 'AFRIPP-2012060818343243PDT' as "REFERENCE"
)
/
--
-- 3. XQuery with multiple predicates, returning a single node (Reference)
--
select *
from XMLTABLE(
'for $i in fn:collection("oradb:/XDBOE/PURCHASEORDER")/PurchaseOrder[CostCenter=$CC and Requestor=$REQUESTOR and count(LineItems/LineItem) > $QUANTITY]/Reference
return $i'
passing 'A60' as "CC", 'Diana Lorentz' as "REQUESTOR", 5 as "QUANTITY"
)
/
--
-- 4. XQuery constructing a new summary document from the documents that match the specified predicates
-- Also demonstrates the use of nested FOR loops, one for the set of PurchaseOrder documents, and one for
-- the LineItem elements
--
select *
from XMLTable(
'<Summary UPC="{$UPC}">
{
for $p in fn:collection("oradb:/XDBOE/PURCHASEORDER")/PurchaseOrder
for $l in $p/LineItems/LineItem[Quantity > $Quantity and Part/text() =$UPC]
order by $p/Reference
return
<PurchaseOrder reference="{$p/Reference/text()}" lineItem="{fn:data($l/@ItemNumber)}" Quantity="{$l/Quantity}"/>
}
</Summary>'
passing '707729113751' as UPC, 3 as "Quantity"
)
/
--
-- 5. Use XMLSerialize to format the XMLType and serialize it as a CLOB.
-- Allows result to be viewed in products that do not support XMLType.
-- XMLSerialize allows control over the layout of the serialized
-- XML.
--
select XMLSERIALIZE(CONTENT COLUMN_VALUE AS CLOB INDENT SIZE=2)
from XMLTable(
'<Summary UPC="{$UPC}">
{
for $p in fn:collection("oradb:/XDBOE/PURCHASEORDER")/PurchaseOrder
for $l in $p/LineItems/LineItem[Quantity > $Quantity and Part/text() =$UPC]
order by $p/Reference
return
<PurchaseOrder reference="{$p/Reference/text()}" lineItem="{fn:data($l/@ItemNumber)}" Quantity="{$l/Quantity}"/>
}
</Summary>'
passing '707729113751' as UPC, 3 as "Quantity"
)
/
--
-- 6. Using XMLTable to create an in-line relational view from the documents that match the XQuery expression.
--
select *
from xmlTable(
'for $p in fn:collection("oradb:/XDBOE/PURCHASEORDER")/PurchaseOrder
for $l in $p/LineItems/LineItem[Quantity > 3 and Part/text() = "707729113751"]
return
<Result ItemNumber="{fn:data($l/@ItemNumber)}">
{
$p/Reference,
$p/Requestor,
$p/User,
$p/CostCenter,
$l/Quantity
}
<Description>{fn:data($l/Part/@Description)}</Description>
<UnitPrice>{fn:data($l/Part/@UnitPrice)}</UnitPrice>
<PartNumber>{$l/Part/text()}</PartNumber>
</Result>'
columns
SEQUENCE for ordinality,
ITEM_NUMBER NUMBER(3) path '@ItemNumber',
REFERENCE VARCHAR2( 30) path 'Reference',
REQUESTOR VARCHAR2(128) path 'Requestor',
USERID VARCHAR2( 10) path 'User',
COSTCENTER VARCHAR2( 4) path 'CostCenter',
DESCRIPTION VARCHAR2(256) path 'Description',
PARTNO VARCHAR2( 14) path 'PartNumber',
QUANTITY NUMBER(12,4) path 'Quantity',
UNITPRICE NUMBER(14,2) path 'UnitPrice'
)
/
--
-- 7. Joining relational and XML tables using XQuery.
--
select REQUESTOR, DEPARTMENT_NAME
from HR.EMPLOYEES e, HR.DEPARTMENTS d,
XMLTABLE(
'for $p in fn:collection("oradb:/XDBOE/PURCHASEORDER")/PurchaseOrder
where $p/User=$EMAIL and $p/Reference=$REFERENCE
return $p'
passing 'AFRIPP-2012060818343243PDT' as "REFERENCE", e.EMAIL as "EMAIL"
COLUMNS
REQUESTOR path 'Requestor/text()',
USERNAME path 'User'
)
where e.DEPARTMENT_ID = d.DEPARTMENT_ID
/
Searching XML Content Using XQuery Full Text
XQuery Full Text is an extension to W3C XQuery standard that makes it perform complex full-text style search operations on the content of XML documents. Support for XQuery Full Text is available in Oracle Database 12.1.0.1.0 and later. The original XQuery language included an operator called CONTAINS that allow pattern-matching-based searches on XML content. The CONTAINS operator provides a case-sensitive substring-style search capability; that is, it returns TRUE if the source string contains exactly the set of characters contained in the target string. The XQuery Full Text specification adds the ability to perform word-based searches of XML content, with all of the common features of a text-retrieval system. XQuery Full Text includes support for word match, windowing (word must appear within n words of word), and stemming (automatically recognize related words). In order to provide an efficient implementation of XQuery Full Text, Oracle Database 12c makes use of Oracle’s Text indexing technology. In order to make use of XQuery Full Text it is necessary to create an XML-aware full-text index on the documents that are to be searched.
This section demonstrates how to create an XML Aware full-text index.
Summary
- Create a simple XMLType table and populate it with 10,000 XML documents, and then view the table in SQL Developer.
- Use XQuery and SQL/XML to query XML documents stored in the PURCHASEORDER table in the XDBOE schema.
- Create an XML-aware full-text index.
- Perform XQuery full-text searches on the PURCHASEORDER table.
- For additional information on Oracle XML DB:
Attend the Oracle Database 12c: Use XML DB 5-day course.
See the Oracle XML DB Developer's Guide 12c Release 1 (12.1) documentation reference. - For additional information on using HTTP on a standard port (80) instead of an Oracle XML DB Default port, see the Using HTTP(S) on a Standard Port Instead of an Oracle XML DB Default Port section (chapter 28) in the Oracle XML DB Developer's Guide 12c Release 1 (12.1) documentation reference.
- For useful XML DB white papers, technical presentations, demos, and hands-on practices, access the Oracle XML DB Home Page.
- To learn more about XML DB , refer to the additional OBEs in the Oracle Learning Library OLL Web site OLL .
- Lead Curriculum Developer: Lauran K. Serhal
- Main Contributor: Mark Drake
In this tutorial, you have learned how to:
Resources
Credits
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- Print:
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.