Session IV Chapter 16 – XQuery http://www.profburnett.com CMP 051 XML Introduction Session IV Chapter 16 – XQuery http://www.profburnett.com
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
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
Composing and XQuery Document xquery version "3.0"; declare namespace ext="http://www.altova.com/xslt-extensions"; "Hello World!" xquery version "3.0"; declare namespace ext="http://www.altova.com/xslt-extensions"; "Everyone count down:", reverse(1 to 10), "Blastoff!" xquery version "3.0"; declare namespace ext="http://www.altova.com/xslt-extensions"; <countdown>{reverse(1 to 10)}</countdown> 8/1/2014 Copyright © Carl M. Burnett
Identifying a Source Document xquery version "3.0"; declare namespace ext="http://www.altova.com/xslt-extensions"; doc ("C:\Student_Exercises\Session_2_Exercise_1\2016_Female_Songs_of_the_Year.xml") 8/1/2014 Copyright © Carl M. Burnett
Using Path Expressions xquery version "3.0"; declare namespace ext="http://www.altova.com/xslt-extensions"; doc ("C:\Student_Exercises\Session_2_Exercise_1\2016_Female_Songs_of_the_Year.xml") /DocumentElement/Song 8/1/2014 Copyright © Carl M. Burnett
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
FLWOR Expression 8/1/2014 Copyright © Carl M. Burnett 2016_Female_Songs_of_the_Year.xml xquery version "3.0"; declare namespace ext="http://www.altova.com/xslt-extensions"; 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
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
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 ($x/@category="CHILDREN") then <child>{data($x/title)}</child> else <adult>{data($x/title)}</adult> $bookstore//book/@q > 10 $bookstore//book/@q gt 10 XQuery Functions 8/1/2014 Copyright © Carl M. Burnett
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($wndr/name[@language='English']), data($quake/date)) 8/1/2014 Copyright © Carl M. Burnett
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
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
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