Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals (Exam 730) Classroom Resources Part 7: Introducing XQuery
© Copyright IBM Corporation 2008 Unit objectives After completing this unit, you should be able to: List fundamental XQuery concepts Write simple XQueries using several common expressions
© Copyright IBM Corporation 2008 Certification Exam (730) objectives Knowledge of XML data implications (non-shredding) Given an XQuery statement, knowledge to identify results
© Copyright IBM Corporation 2008 Sample XML document
© Copyright IBM Corporation 2008 XQuery navigation Navigate to the city element /Client/Address/city More path expressions: //* // /Client/ [1]/text() /Client/Address/* /Client/Address[state="CA"]/../
© Copyright IBM Corporation 2008 Sample data for the clients table INSERT INTO clients VALUES (3227, 'Ella Kimpton', 'Gold', ' 5401 Julio Ave. San Jose CA ' );
© Copyright IBM Corporation 2008 Simple XML data retrieval Simple XQuery to return customer contact data XQUERY db2-fn:xmlcolumn('CLIENTS.CONTACTINFO') Output from the query: 5401 Julio Ave. San Jose CA
© Copyright IBM Corporation 2008 Specifying a single filtering predicate FLWOR expression with a new WHERE clause XQUERY FOR $y IN db2-fn:xmlcolumn('CLIENTS.CONTACTINFO') /Client/Address WHERE $y/zip="95116" RETURN $y Output from the query: 5401 Julio Ave. San Jose CA 95116
© Copyright IBM Corporation 2008 Converting XML to HTML Querying DB2 XML data and returning results as HTML: XQUERY { FOR $y IN db2-fn:xmlcolumn('CLIENTS.CONTACTINFO') /Client/Address ORDER BY $y/zip RETURN {$y} } HTML output of the query: 9407 Los Gatos Blvd. Los Gatos CA El Camino Real Mountain View CA
© Copyright IBM Corporation 2008 Conditional logic XQuery with a three-part conditional expression FOR $y IN db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client RETURN ( IF ($y/ ) THEN $y/ [1] ELSE IF ($y/phone/home) THEN {$y/phone/home/text()} ELSE $y/Address) Query output: 1204 Meridian Ave. 4A San Jose CA
© Copyright IBM Corporation 2008 Hybrid queries Embedding SQL within an XQuery XQUERY FOR $y IN db2-fn:sqlquery ('SELECT contactinfo FROM clients WHERE status=''Gold'' ')/Client WHERE $y/Address/city="San Jose" RETURN ( IF ($y/ ) THEN {$y/ } ELSE $y/Address ) Query output 1204 Meridian Ave. 4A San Jose CA 95124
© Copyright IBM Corporation 2008 Unit summary Having completed this unit, you should be able to: List fundamental XQuery concepts Write simple XQueries using several common expressions