Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session II Chapter 16 – XQuery

Similar presentations


Presentation on theme: "Session II Chapter 16 – XQuery"— Presentation transcript:

1 Session II Chapter 16 – XQuery http://www.profburnett.com
CMP 051 XML Introduction Session II Chapter 16 – XQuery

2 Outline XQuery Introduction XQuery Functions
Composing and XQuery Document Identifying a Source Document Using Path Expressions FLWOR Expression XQuery Syntax XQuery Functions Add Select Conditional Expressions User Defined Functions Joining Data Sources XQuery and Databases 8/1/2014 Copyright © Carl M. Burnett

3 XQuery Introduction XQuery is to XML what SQL is to database tables.
XQuery is designed to query XML data Extract information to use in a Web Service Generate summary reports Transform XML data to XHTML Search Web documents for relevant information 8/1/2014 Copyright © Carl M. Burnett

4 Composing and XQuery Document
xquery version "1.0"; "Hello World!" xquery version "1.0"; "Everyone count down:", reverse(1 to 10), "Blastoff!" xquery version "1.0"; <countdown>{reverse(1 to 10)}</countdown> 8/1/2014 Copyright © Carl M. Burnett

5 Identifying a Source Document
xquery version "1.0"; doc("wonders-master.xml") 8/1/2014 Copyright © Carl M. Burnett

6 Using Path Expressions
xquery version "1.0"; doc("wonders-master.xml")/ancient_wonders/wonder/location 8/1/2014 Copyright © Carl M. Burnett

7 FLWOR Expression FLWOR - "For, Let, Where, Order by, Return".
“for” clause selects elements into a variable called $x. “where” clause selects only elements with a specified. “order” clause defines the sort-order. “return” clause specifies what should be returned. 8/1/2014 Copyright © Carl M. Burnett

8 FLWOR Expression Books.xml wonders-master.xml xquery version "1.0";
for $wndr in doc("wonders-master.xml")/ancient_wonders/wonder where contains($wndr/location, "Turkey") return $wndr/name Books.xml xquery version "1.0"; for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title 8/1/2014 Copyright © Carl M. Burnett

9 XQuery Syntax XQuery is case-sensitive
XQuery elements, attributes, and variables must be valid XML names An XQuery string value can be in single or double quotes An XQuery variable is defined with a $ followed by a name, e.g. $bookstore XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :) 8/1/2014 Copyright © Carl M. Burnett

10 XQuery Functions XQuery Conditional Expressions XQuery Comparisons
1. General comparisons: =, !=, <, <=, >, >= 2. Value comparisons: eq, ne, lt, le, gt, ge for $x in doc("books.xml")/bookstore/book return if then <child>{data($x/title)}</child> else <adult>{data($x/title)}</adult> > 10 gt 10 XQuery Functions 8/1/2014 Copyright © Carl M. Burnett

11 Joining Data Sources xquery version "1.0"; for $wndr in
doc("wonders-master.xml")/ancient_wonders/wonder for $quake in doc("earthquake_data.xml")/earthquakes/occurrence where $quake[cities_affected/city = $wndr/location] return data($quake/date)) 8/1/2014 Copyright © Carl M. Burnett

12 XQuery and Databases xquery version "1.0";
let $cntry_medals := collection("olympics.medals")/* let $total_gold := sum(data($cntry_medals/gold)) let $total_silver := sum(data($cntry_medals/silver)) let $total_bronze := sum(data($cntry_medals/bronze)) let $total_medals := $total_gold + $total_silver + $total_bronze return ( "Total gold medals awarded:", $total_gold, "Total silver medals awarded:", $total_silver, "Total bronze medals awarded:", $total_bronze, "Total medals awarded:", $total_medals ) 8/1/2014 Copyright © Carl M. Burnett

13 Review XQuery Introduction XQuery Functions
Composing and XQuery Document Identifying a Source Document Using Path Expressions FLWOR Expression XQuery Syntax XQuery Functions Add Select Conditional Expressions User Defined Functions Joining Data Sources XQuery and Databases Next – Chapter 14 - XSLT 2.0 8/1/2014 Copyright © Carl M. Burnett


Download ppt "Session II Chapter 16 – XQuery"

Similar presentations


Ads by Google