Parentheses and unions

You can also use parentheses to group operations within a step. For example, the following query returns all book titles in the bookstore.

select xmlextract(’/bookstore/book/title’, text_doc) from sample_docs where name_doc=’bookstore’
---------------------------------------
<title>Seven Years in Trenton</title>
<title>History of Trenton</title>
<title>Trenton Today, Trenton Tomorrow</title>
<title>Who’s Who in Trenton</title>

The above query returns only book titles. To return magazine titles, change the query to:

select xmlextract(’/bookstore/magazine/title’, text_doc) 
from sample_docs where name_doc=’bookstore’
---------------------------------------
<title>Tracking Trenton</title>

To return the titles of all items in the bookstore, you could change the query as follows:

select xmlextract(’/bookstore/*/title’, text_doc) 
from sample_docs where name_doc=’bookstore’
----------------------------------
<title>Seven Years in Trenton</title>
<title>History of Trenton</title>
<title>Tracking Trenton</title>
<title>Trenton Today, Trenton Tomorrow</title>
<title>Whos Who in Trenton</title>

If the bookstore contains elements other than books and magazines—such as calendars and newspapers—you can query only for book and magazine titles by using the union (vertical bar) operator, and parenthesizing it in the query path. For example:

select xmlextract(’/bookstore/(book|magazine)/title’, text_doc) 
from sample_docs where name_doc=’bookstore’
----------------------------------
<title>Seven Years in Trenton</title>
<title>History of Trenton</title>
<title>Tracking Trenton</title>
<title>Trenton Today, Trenton Tomorrow</title>
<title>Whos Who in Trenton</title>