msgbartop
News, views, tips and tricks on Oracle and other fun stuff
msgbarbottom

XML Goodness in SQL – Part 2

In part 1, we reviewed HTTPURIType and DBURIType. Let’s now go over a few examples of more advanced XML using the SQL functions XMLTable and XMLQuery, Oracle’s native support for XQuery.

SQL functions XMLQuery and XMLTable are defined by the SQL/XML standard as a general interface between the SQL and XQuery languages. XMLQuery and XMLTable let you take advantage of the power and flexibility of both SQL and XML. Using these functions, you can construct XML data using relational data, query relational data as if it were XML, and construct relational data from XML data.

Let’s query blog titles from the OraNA.info RSS feed without using XQuery:

SQL> SELECT value(i).extract('/item/title/text()'
  2         ,'xmlns:dc="http://purl.org/dc/elements/1.1/"' ).getstringval() title
  3  FROM   TABLE (XMLSequence(
  4         HTTPURITYPE('http://feeds.feedburner.com/orana').getxml().extract('//item'))) i;

TITLE
--------------------------------------------------------------------------------
Oracle ANZ Blogosphere Highlights
Taking the Plunge: Part 2
8 Things about Louise
Working with PHP and Oracle Presentation
Removing Oracle Database XE from Linux
...

Starting with Oracle database 10gR2 you have the option to use the XQuery SQL function XMLTable. You use XMLTable to shred the result of an XQuery-expression evaluation into the relational rows and columns of a new, virtual table. You can then insert the virtual table into a pre-existing database table, or you can query it using SQL. You use XMLTable in a SQL FROM clause.

Let’s change the query above to use XMLTable and return the blog author as well:

SQL> SELECT *
  2      FROM XMLTable (
  3        '//item'
  4        passing HTTPURITYPE('http://feeds.feedburner.com/orana').getXML()
  5        COLUMNS title   varchar2(4000) path '/item/title/text()',
  6                creator varchar2(4000) path
  7                        '/item/*[namespace-uri()="http://purl.org/dc/elements/1.1/"
  8                                and local-name()="creator"]/text()'
  9     );

TITLE                                    CREATOR
---------------------------------------- --------------------
Oracle ANZ Blogosphere Highlights        Carl Terrantroy
Taking the Plunge: Part 2                Jake
8 Things about Louise                    Louise Barnfield
Working with PHP and Oracle Presentation alison.holloway
Removing Oracle Database XE from Linux   alison.holloway
...

And here is another example that selects the entries from the Oracle room on FriendFeed:

SQL> SELECT *
  2    FROM XMLTable (
  3        '/feed/entry'
  4        passing HTTPURIType ('http://friendfeed.com/api/feed/room/oraclestuff?format=xml').getXML()
  5        columns title varchar2(4000) path 'title/text()'
  6   );

TITLE
----------------------------------------
Oracle ECM and Skywire Database modeling support in SQL Developer
Oracle Fusion Middleware Strategy Webcast Replay
...

You can also use the XQuery function ora:view within an XQuery expression to query a relational table or view as if it were XML. Here is a simple example using the table we created in part 1:

SQL> SELECT *
  2    FROM XMLTable(
  3           'for $i in ora:view("t")
  4                   return $i');


COLUMN_VALUE
---------------------------------------

<ROW><A>1</A><B>Eddie</B></ROW>
<ROW><A>2</A><B>John</B></ROW>
...

The other XQuery function is XMLQuery. You use it to construct or query XML data. This function takes as arguments an XQuery expression, and an optional XQuery context item. The context item establishes the XPath context in which the XQuery expression is evaluated. Additionally, XMLQuery accepts as arguments any number of SQL expressions whose values are bound to XQuery variables during the XQuery expression evaluation. The function returns the result of evaluating the XQuery expression, as an XMLType instance.

Here are a couple of simple examples:

SQL> SELECT XMLQuery('(100 to 105)'
  2                  RETURNING CONTENT) AS output
  3    FROM DUAL;

OUTPUT
-------------------------------------------------

100 101 102 103 104 105

SQL> SELECT XMLQuery('for $i in ora:view("t")
  2                   return <ROWS>{$i}</ROWS>'
  3                  RETURNING CONTENT) AS col
  4    FROM DUAL;

COL
---------------------------------------------------
<ROWS><ROW><A>1</A><B>Eddie</B></ROW></ROWS><ROWS><

The above examples give you a “taste” of the capabilities of XQuery in Oracle. For more detailed information please refer to the following list of resources:


Filed in Oracle on 09 Jul 08 | Tags: , ,


Reader's Comments

  1. |

    Interesting Post