Bringing Together the Power of SQL and XML with XQuery
The XQuery language provides an efficient means of querying, constructing, and transforming XML data. Using XQuery, you can easily bring together pieces of information from disparate data sources, accessing both database and non-database data within the same query. The only requirement is that the data you’re accessing must be expressible in XML format. Say, it may be relational data, office documents, or simply plain text files.
Oracle Database provides a standard-compliant XQuery implementation making it possible for you to access XML-presentable data wherever it is found: stored in a database, located in a file system, or even retrieved by a search engine. For example, you might issue an XQuery query that will construct XML from relational data or will transform an XML document stored in a file system into HTML code.
Using Oracle XQuery is straightforward. You pass an XQuery expression as the argument to a SQL/XML standard function such as XMLQuery or XMLTable, thus taking advantage of XQuery through standard SQL interfaces. Being used as the argument of an SQL function invoked within an SQL statement, an XQuery query returns either XML or relational data that you can further process in the parent SQL query. For example, you might define an SQL join query that will connect a Google search’s result decomposed into relational rows by XMLTable, with the data stored in a relational table, thus seamlessly integrating relational and Web data.
Just like with regular SQL statements, you can improve flexibility and execution performance of XQuery expressions by using bind dynamic variables. Using bind variables reduces parsing overhead, thus increasing your performance and scalability as well. You can bind dynamic variables to an XQuery expression within the PASSING clause, which is available in both the XMLQuery and XMLTable SQL functions.
It’s interesting to note that Oracle Database allows you to monitor the performance of XQuery expressions, examining the execution path used by the SQL optimizer in the same way you would do it when dealing with a regular SQL statement. In Oracle SQL*Plus, you set the AUTOTRACE system variable to print the execution path used by the SQL optimizer. For further details, you can refer to the SQL*Plus User’s Guide and Reference book in the Oracle Database documentation.
As you no doubt have realized, XQuery is similar to SQL but is not limited to working only with database data – any data source expressible in XML can be reached with your XQuery query.