XML Query (Xquery) Support in Oracle Database 10G Release 2
Total Page:16
File Type:pdf, Size:1020Kb
XML Query (XQuery) Support in Oracle Database 10g Release 2 An Oracle White Paper May 2005 XML Query (XQuery) Support in Oracle Database 10g Release 2 Introduction ....................................................................................................... 3 Note on Future Compatibility .................................................................... 4 The Structure of XQuery............................................................................. 5 Database or Mid-Tier?...................................................................................... 6 SQL and XQuery .............................................................................................. 7 Differences between SQL and XQuery .................................................. 15 Using XQuery in the Database...................................................................... 15 Querying XML Documents in the Oracle XML DB Repository........ 16 Full-Text Searches of XML .................................................................. 19 Querying a Relational Table or View as if it were XML Data ............. 21 Querying Native XMLType data in Oracle XML DB .......................... 22 Querying Across Databases and Filesystems, or Websites................... 24 Some Interesting Cases .............................................................................. 27 Type Checking........................................................................................ 27 Namespaces............................................................................................. 28 Performance and Scalability: Co-processor vs. Native Compilation....... 29 Storage Optimization ................................................................................. 29 Intra-Query Optimization ......................................................................... 29 Inter-Query Optimization ......................................................................... 29 Beyond Co-Processor: Oracle’s Native XQuery Implementation ...... 30 Areas of Limited Support............................................................................... 31 Implementation-specific choices .............................................................. 31 Implementation Departures from the XQuery Standard ..................... 32 Support for XQuery Functions and Operators...................................... 32 Benefits of XQuery......................................................................................... 32 Conciseness and Simplicity........................................................................ 32 Heterogeneous Queries.............................................................................. 34 Leveraging the XML Data Model ............................................................ 34 XML Construction ..................................................................................... 34 XQuery vs. XSLT................................................................................... 36 The Database XQuery Environment ........................................................... 36 Top Level Query Support through SQL*Plus ....................................... 36 XDK............................................................................................................. 36 XQuery in the Mid-Tier ................................................................................. 37 XQJ............................................................................................................... 37 Query Pushdown ........................................................................................ 37 Using XQuery in the Mid-Tier ................................................................. 38 Querying a RSS Feed............................................................................. 38 Oracle XML Query Services ..................................................................... 39 Conclusion........................................................................................................ 40 XML Query (XQuery) Support in Oracle Database 10g Release 2 Page 2 XML Query (XQuery) Support in Oracle Database 10g Release 2 INTRODUCTION XQuery makes its debut in Oracle XML Query (XQuery) is a standards effort undertaken by the World Wide Web Database 10g Release 2. consortium to enable data to be extracted from XML documents. XQuery 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. XQuery is also useful for constructing XML data and documents as the return result of query expressions. 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 Release2 debuts a full-featured native XQuery engine integrated with the traditional Oracle database server to help organizations explore their XQuery needs. With Oracle Database 9i Release 2, we introduced a native XML store – XML DB – integrated with and part of the traditional Oracle Database. XML DB provides a high-fidelity storage and retrieval of XML documents, and is useful for a certain classes of data-management tasks, viz.: • Content Management – standards-based life-cycle management of content expressed in XML (e.g. technical manuals, multi-media messages, legal statutes, regulatory filings) • Exchange and Storage – generation of template-based business documents expressed as XML (e.g. purchase orders, bills, invoices, reports) to exchange between applications, and storage of such documents natively in the database • Data Integration – querying across different types of information assets – database records, files, web servers, news feeds. With 10g Release 1 and Release 2, the rapidly maturing XML DB technologies included with the Oracle Database are being used for a variety of applications which incorporate the above tasks, such as: • Product Documentation and Technical Manuals XML Query (XQuery) Support in Oracle Database 10g Release 2 Page 3 • Product Specification and Design Documents • Web Site Content • News and Information Services • Legislative Documents • Contracts • Financial Reporting • Document Publishing. From the perspective of developers building such applications, the initial use of XML as a storage format is broadening into the need to query the stored XML. The common reasons typically cited for using XQuery to query XML are: • Conciseness and simplicity – it is estimated that accessing XML data through XQuery results in one-fifth the code of XSLT-oriented approaches and one-twentieth the code of DOM-based approaches • Heterogeneous queries – since XML can map to a large number of different data models, one could search databases, files, web-services simultaneously using XQuery. • XML data model – XQuery is based on XML Schema. The XML Schema- based data model is best suited for representing variable, unpredictable, and irregular – i.e. ‘semi-structured’ data. When querying such data, XQuery is an obvious choice. • XML construction – XQuery can construct XML as the result of evaluating query expressions, in many cases more expressively and efficiently than XSLT. Note on Future Compatibility Since XQuery is not yet a final W3C At the time of Oracle Database 10g Release 2, XQuery is in the final stages of recommendation, the current support is becoming a World Wide Web Consortium (W3C) recommendation. The Oracle not guaranteed to be backwards- Database 10g Release2 supports the April 2005 version of the XQuery language compatible. specifications. It is possible that the final standard might be different from the late- stage versions. Oracle will continue to track the evolution of the XQuery standard, until it becomes a recommendation. During this period, Oracle does not guarantee backward compatibility of your XQuery code. After the XQuery standard becomes a recommendation, Oracle will produce a release of Oracle Database that is compatible with the recommendation. From that point on, standard Oracle policies will apply to the Oracle XQuery implementation. XML Query (XQuery) Support in Oracle Database 10g Release 2 Page 4 The Structure of XQuery As a brief introduction to XQuery, let us assume we are operating on a simple document named emp.xml: <empset> <emp empno=—21“ ename=—SCOTT“ salary=—50000“/> <emp empno=—22“ ename=—JONES“ salary=—100000“/> </empset> This document could be stored as a file, or generated as virtual XML document out of an Oracle database. To get the names of those employees with salary > 80000, the following XQuery fragment can be used: for $i in document(”emp.xml‘)/empset let $j = 80000 where $i/@salary > $j return $i/@ename The result is the following attribute node: JONES Let us look at the basic structure of XQuery. The language consists of the following major constructs. For -- Analogous to the FROM clause of a SQL SELECT statement, the For clause lets you iterate across a range of sequence values Let – Analogous to the SQL SET statement, the Let clause lets you define variables and assign them in turn during iteration through a FOR clause. Where -- Analogous to the WHERE clause of a SQL SELECT statement, the Where clause provides a set of conditions that filter or limit the initial selection in a For