Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session IV Chapter 16 – XQuery

Similar presentations


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

1 Session IV Chapter 16 – XQuery http://www.profburnett.com
CMP 051 XML Introduction Session IV 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 "3.0"; declare namespace ext=" "Hello World!" xquery version "3.0"; declare namespace ext=" "Everyone count down:", reverse(1 to 10), "Blastoff!" xquery version "3.0"; declare namespace ext=" <countdown>{reverse(1 to 10)}</countdown> 8/1/2014 Copyright © Carl M. Burnett

5 Identifying a Source Document
xquery version "3.0"; declare namespace ext=" doc ("C:\Student_Exercises\Session_2_Exercise_1\2016_Female_Songs_of_the_Year.xml") 8/1/2014 Copyright © Carl M. Burnett

6 Using Path Expressions
xquery version "3.0"; declare namespace ext=" doc ("C:\Student_Exercises\Session_2_Exercise_1\2016_Female_Songs_of_the_Year.xml") /DocumentElement/Song 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 8/1/2014 Copyright © Carl M. Burnett
2016_Female_Songs_of_the_Year.xml xquery version "3.0"; declare namespace ext=" for $album_song in ("C:\Student_Exercises\Session_2_Exercise_1\2016_Female_Songs_of_the_Year.xml") /DocumentElement/Song let $songt := $album_song /Track_Name where contains ($album_song /Artist_Name, "Birdy") order by $album_song /Album_Name return 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 Student Exercise 1 Create a XQuery for the 2016 Female Songs of the Year.xml file using Altova XMLSpy. Preview Transformed XQuery Output.XML file in browser. Link XQuery files to your webpage. FLOWR Xquery (.xqu) File XQuery Output.XML file Post to live server. Preview on live site. 8/1/2014 Copyright © Carl M. Burnett

14 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 IV Chapter 16 – XQuery"

Similar presentations


Ads by Google