8.2 W3C XML Query Language
description
Transcript of 8.2 W3C XML Query Language
SDPL 2001 Notes 8.2: XQuery 1
8.2 W3C XML Query Language8.2 W3C XML Query Language
– Thanks for Helena Ahonen-Myka (University of Thanks for Helena Ahonen-Myka (University of Helsinki) for borrowing her slide originals for Helsinki) for borrowing her slide originals for this sectionthis section
SDPL 2001 Notes 8.2: XQuery 2
XML Query languageXML Query language
W3C 15.2.2001: (early) working draftsW3C 15.2.2001: (early) working drafts– XML Query RequirementsXML Query Requirements– A Data modelA Data model– XML Query AlgebraXML Query Algebra– XQuery: A Query Language for XMLXQuery: A Query Language for XML
influenced by the work of many research groups influenced by the work of many research groups and query languagesand query languages
goal: a query language that is broadly applicable goal: a query language that is broadly applicable across all types of XML data sourcesacross all types of XML data sources
SDPL 2001 Notes 8.2: XQuery 3
Usage scenariosUsage scenarios
Human-readable documentsHuman-readable documents– perform queries on structured documents and perform queries on structured documents and
collections of documents, such as technical manuals,collections of documents, such as technical manuals,» to retrieve individual documents, to retrieve individual documents, » to generate tables of contents, to generate tables of contents, » to search for information in structures found within a to search for information in structures found within a
document, or document, or » to generate new documents as the result of a queryto generate new documents as the result of a query
SDPL 2001 Notes 8.2: XQuery 4
Usage scenariosUsage scenarios
Data-oriented documentsData-oriented documents– perform queries on the XML representation of database perform queries on the XML representation of database
data, object data, or other traditional data sourcesdata, object data, or other traditional data sources» to extract data from these sourcesto extract data from these sources» to transform data into new XML representationsto transform data into new XML representations» to integrate data from multiple heterogeneous data sourcesto integrate data from multiple heterogeneous data sources
– the XML representation of data sources may be either the XML representation of data sources may be either physical or virtual:physical or virtual:
» data may be physically encoded in XML, or an XML data may be physically encoded in XML, or an XML representation of the data may be producedrepresentation of the data may be produced
SDPL 2001 Notes 8.2: XQuery 5
Usage scenariosUsage scenarios
Mixed-model documentsMixed-model documents– perform both document-oriented and data-oriented perform both document-oriented and data-oriented
queries on documents with embedded data, such as queries on documents with embedded data, such as catalogs, patient health records, employment recordscatalogs, patient health records, employment records
Administrative dataAdministrative data– perform queries on configuration files, user profiles, or perform queries on configuration files, user profiles, or
administrative logs represented in XMLadministrative logs represented in XML
Native XML repositories (databases)Native XML repositories (databases)
SDPL 2001 Notes 8.2: XQuery 6
Usage scenariosUsage scenarios
Filtering streamsFiltering streams– perform queries on streams of XML data to process the perform queries on streams of XML data to process the
data (logs of email messages, network packets, stock data (logs of email messages, network packets, stock market data, newswire feeds, EDI) market data, newswire feeds, EDI)
» to filter and route messages represented in XMLto filter and route messages represented in XML» to extract data from XML streamsto extract data from XML streams» to transform data in XML streamsto transform data in XML streams
DOMDOM– perform queries on DOM structures to return sets of perform queries on DOM structures to return sets of
nodes that meet the specified criterianodes that meet the specified criteria
SDPL 2001 Notes 8.2: XQuery 7
Usage scenariosUsage scenarios
Multiple syntactic environmentsMultiple syntactic environments– queries may be used in many environmentsqueries may be used in many environments– a query might be embedded in a URL, an XML page, or a query might be embedded in a URL, an XML page, or
a JSP or ASP pagea JSP or ASP page– represented by a string in a program written in a represented by a string in a program written in a
general-purpose programming languagegeneral-purpose programming language– provided as an argument on the command-line or provided as an argument on the command-line or
strandard inputstrandard input
SDPL 2001 Notes 8.2: XQuery 8
RequirementsRequirements
Query language syntaxQuery language syntax– the XML Query Language may have more than one the XML Query Language may have more than one
syntax bindingsyntax binding» one must be convenient for humans to read and writeone must be convenient for humans to read and write» one must be expressed as XML reflecting the underlying one must be expressed as XML reflecting the underlying
structure of the querystructure of the query
DeclarativityDeclarativity– the language must be declarativethe language must be declarative– it must not enforce a particular evaluation strategyit must not enforce a particular evaluation strategy
SDPL 2001 Notes 8.2: XQuery 9
RequirementsRequirements
Reliance on XML Information SetReliance on XML Information Set– the XML Query Data Model relies on information the XML Query Data Model relies on information
provided by XML and Schema Processors provided by XML and Schema Processors – not allowed to depend on information that is not not allowed to depend on information that is not
made available by such processorsmade available by such processors DatatypesDatatypes
– the data model must represent both XML 1.0 the data model must represent both XML 1.0 character data and the simple and complex types of character data and the simple and complex types of the XML Schema specificationthe XML Schema specification
Schema: Schema: – taken into acount, but not required for queriestaken into acount, but not required for queries
SDPL 2001 Notes 8.2: XQuery 10
Requirements: functionalityRequirements: functionality
Support operations (selection, projection, aggregation, Support operations (selection, projection, aggregation, sorting, etc.) on all data types: sorting, etc.) on all data types: – Choose a part of the data based on content or structureChoose a part of the data based on content or structure– Also operations on hierarchy and order of document structuresAlso operations on hierarchy and order of document structures
Structural preservation and transformation:Structural preservation and transformation: – Preserve the relative hierarchy and sequence of input document Preserve the relative hierarchy and sequence of input document
structures in the query resultsstructures in the query results– Transform XML structures and create new XML structuresTransform XML structures and create new XML structures
Combination and joining:Combination and joining:– Combine related information from different parts of a given Combine related information from different parts of a given
document or from multiple documentsdocument or from multiple documents
SDPL 2001 Notes 8.2: XQuery 11
Requirements: functionalityRequirements: functionality
References:References:– Queries must be able to traverse intra- and inter-document Queries must be able to traverse intra- and inter-document
references references
Closure property:Closure property: – The result of an XML query is also XML (usually not a valid The result of an XML query is also XML (usually not a valid
document, but a well-formed document document, but a well-formed document fragmentfragment))– The results of a query can be used as input to another queryThe results of a query can be used as input to another query
Extensibility:Extensibility:– The query language should support the use of externally The query language should support the use of externally
defined functions on all datatypes of the data modeldefined functions on all datatypes of the data model
SDPL 2001 Notes 8.2: XQuery 12
XQueryXQuery
Design goals:Design goals:– a small, easily implementable languagea small, easily implementable language– queries are concise and easily understoodqueries are concise and easily understood– flexible enough to query a broad spectrum of XML flexible enough to query a broad spectrum of XML
information sources (incl. both databases and information sources (incl. both databases and documents)documents)
– a human-readable query syntax (XML-based query a human-readable query syntax (XML-based query syntax coming later…)syntax coming later…)
features borrowed from many languagesfeatures borrowed from many languages– Quilt, XPath, XQL, XML-QL, SQL, OQL, Lorel, ...Quilt, XPath, XQL, XML-QL, SQL, OQL, Lorel, ...
SDPL 2001 Notes 8.2: XQuery 13
XQuery vs. another XML activitiesXQuery vs. another XML activities
Semantics of XQuery are defined in terms of the Semantics of XQuery are defined in terms of the operators of the XML Query Algebraoperators of the XML Query Algebra
type system is the type system of XML Schematype system is the type system of XML Schema path expressions (for navigating in hierarchic path expressions (for navigating in hierarchic
documents) ~ path expressions of XPathdocuments) ~ path expressions of XPath
SDPL 2001 Notes 8.2: XQuery 14
XQueryXQuery
A query is represented as an expression A query is represented as an expression expressions can be nested with full generalityexpressions can be nested with full generality the input and output of a query are instances of a the input and output of a query are instances of a
data model called the XML Query Data Model ~ data model called the XML Query Data Model ~ the data model of XPath (tree of nodes)the data model of XPath (tree of nodes)– a fragment of a document or a collection of documents a fragment of a document or a collection of documents
may lack a common root and may be modeled as an may lack a common root and may be modeled as an ordered forest of nodesordered forest of nodes
SDPL 2001 Notes 8.2: XQuery 15
An instance of the XML Query Model - An instance of the XML Query Model - an ordered forestan ordered forest
SDPL 2001 Notes 8.2: XQuery 16
XQuery expressionsXQuery expressions
path expressionspath expressions element constructorselement constructors FLWR (”flower”; for-let-where-return) expressionsFLWR (”flower”; for-let-where-return) expressions expressions involving operators and functionsexpressions involving operators and functions conditional expressionsconditional expressions quantified expressionsquantified expressions list constructorslist constructors expressions that test or modify datatypesexpressions that test or modify datatypes
SDPL 2001 Notes 8.2: XQuery 17
Path expressionsPath expressions
Use abbraviated syntax of XPathUse abbraviated syntax of XPath the result of a path expression is an ordered list of the result of a path expression is an ordered list of
nodesnodes– each node includes its descendant nodes -> the result each node includes its descendant nodes -> the result
is an ordered forestis an ordered forest
the top-level nodes in the result are ordered the top-level nodes in the result are ordered according to their position in the original hierarchy according to their position in the original hierarchy (in top-down, left-right order)(in top-down, left-right order)
no duplicate nodesno duplicate nodes
SDPL 2001 Notes 8.2: XQuery 18
Element constructorsElement constructors
Similar to XSLT:Similar to XSLT: consists of a start tag and an end tag, enclosing consists of a start tag and an end tag, enclosing
an optional list of expressions that provide the an optional list of expressions that provide the content of the elementcontent of the element– the start tag may also specify the values of one of more the start tag may also specify the values of one of more
attributesattributes– the name of the tag may be specified either by a the name of the tag may be specified either by a
constant or a variableconstant or a variable typical use: typical use:
– nested inside another expression that binds variables nested inside another expression that binds variables that are used in the element constructorthat are used in the element constructor
SDPL 2001 Notes 8.2: XQuery 19
ExampleExample
Generate an Generate an empemp element containing an element containing an empidempid attribute and nested attribute and nested namename and and jobjob elements. The elements. The values of the attribute and nested elements are values of the attribute and nested elements are specified elsewhere.specified elsewhere.
<emp empid = $id><emp empid = $id>
<name> $n </name> ,<name> $n </name> ,
<job> $j </job><job> $j </job>
</emp></emp>
SDPL 2001 Notes 8.2: XQuery 20
ExampleExample
Generate an element with a computed name, Generate an element with a computed name, containing nested elements named containing nested elements named description description and and priceprice
<$tagname><$tagname>
<description> $d <description> $d </description> ,</description> ,
<price> $p </price><price> $p </price>
</$tagname></$tagname>
SDPL 2001 Notes 8.2: XQuery 21
FLWR expressionsFLWR expressions
Constructed from FOR, LET, WHERE, and Constructed from FOR, LET, WHERE, and RETURN clausesRETURN clauses
~SQL select-from-where~SQL select-from-where clauses must appear in a specific orderclauses must appear in a specific order
– 1. FOR/LET, 2. WHERE, 3. RETURN1. FOR/LET, 2. WHERE, 3. RETURN
a FLWR expression binds values to one or more a FLWR expression binds values to one or more variables and then uses these variables to variables and then uses these variables to construct a result (in general, an ordered forest of construct a result (in general, an ordered forest of nodes)nodes)
SDPL 2001 Notes 8.2: XQuery 22
Flow of data in a FLWR expressionFlow of data in a FLWR expression
SDPL 2001 Notes 8.2: XQuery 23
FOR clausesFOR clauses
A FOR clause introduces one or more variables, A FOR clause introduces one or more variables, associating each variable with an expression that associating each variable with an expression that returns a list of nodes (e.g. a path expression)returns a list of nodes (e.g. a path expression)
the result of a FOR clause is a list of tuples, each the result of a FOR clause is a list of tuples, each of which contains a binding for each of the of which contains a binding for each of the variablesvariables
each variable in a FOR clause can be though of each variable in a FOR clause can be though of as iterating over the nodes returned by its as iterating over the nodes returned by its respective expressionrespective expression
SDPL 2001 Notes 8.2: XQuery 24
LET clausesLET clauses
A LET clause is also used to bind one or more A LET clause is also used to bind one or more variables to one or more expressionsvariables to one or more expressions
a LET clause binds each variable to the value of a LET clause binds each variable to the value of its respective expression without iterationits respective expression without iteration
results in binding a single list for each variableresults in binding a single list for each variable Compare:Compare:
– FOR $x IN /library/bookFOR $x IN /library/book -> many bindings (books) -> many bindings (books)– LET $x := /library/bookLET $x := /library/book -> a single list (of books) -> a single list (of books)
SDPL 2001 Notes 8.2: XQuery 25
FOR/LET clausesFOR/LET clauses
A FLWR expression may contain several FOR and A FLWR expression may contain several FOR and LET clausesLET clauses– each of these clauses may contain references to variables each of these clauses may contain references to variables
bound in previous clausesbound in previous clauses the result of the FOR/LET sequence:the result of the FOR/LET sequence:
– an ordered list of tuples of bound variablesan ordered list of tuples of bound variables the number of tuples generated by the FOR/LET the number of tuples generated by the FOR/LET
sequence:sequence:– the product of the cardinalities of the node-lists returned by the product of the cardinalities of the node-lists returned by
the expressions in the FOR clausesthe expressions in the FOR clauses
SDPL 2001 Notes 8.2: XQuery 26
WHERE clauseWHERE clause
Each of the binding tuples generated by the FOR and Each of the binding tuples generated by the FOR and LET clauses can be filtered by an optional WHERE LET clauses can be filtered by an optional WHERE clauseclause
only those tuples for which the condition in the only those tuples for which the condition in the WHERE clause is true are used to invoke the WHERE clause is true are used to invoke the RETURN clauseRETURN clause
the WHERE clause may contain several predicates the WHERE clause may contain several predicates connected by AND, OR, and NOTconnected by AND, OR, and NOT– predicates usually contain references to the bound predicates usually contain references to the bound
variables variables
SDPL 2001 Notes 8.2: XQuery 27
WHERE clauseWHERE clause
Variables bound by a FOR clause represent a Variables bound by a FOR clause represent a single nodesingle node scalar predicates, e.g. scalar predicates, e.g. $p/color = ”Red”$p/color = ”Red”
Variables bound by a LET clause may represent Variables bound by a LET clause may represent lists of nodeslists of nodes list-oriented predicates, e.g. list-oriented predicates, e.g. avg($p/price) > 100avg($p/price) > 100
SDPL 2001 Notes 8.2: XQuery 28
RETURN clauseRETURN clause
The RETURN clause generates the output of the The RETURN clause generates the output of the FLWR expressionFLWR expression– a node, an ordered forest of nodes, primitive valuea node, an ordered forest of nodes, primitive value
is executed on each binding tupleis executed on each binding tuple contains an expression that often contains contains an expression that often contains
element constuctors, references to bound element constuctors, references to bound variables, and nested subexpressionsvariables, and nested subexpressions
SDPL 2001 Notes 8.2: XQuery 29
ExamplesExamples
Assume: a document named ”Assume: a document named ”bib.xmlbib.xml”” The document consists of a list of The document consists of a list of bookbooks:s:
<book><book>++
<title><title><author><author>++
<publisher><publisher><year><year><price><price>
SDPL 2001 Notes 8.2: XQuery 30
List the titles of books published by List the titles of books published by Morgan Kaufmann in 1998Morgan Kaufmann in 1998
FORFOR $b $b ININ documentdocument(”bib.xml”)//book(”bib.xml”)//book
WHEREWHERE $b/publisher = ”Morgan Kaufmann” $b/publisher = ”Morgan Kaufmann”
ANDAND $b/year = ”1998” $b/year = ”1998”
RETURNRETURN $b/title $b/title
SDPL 2001 Notes 8.2: XQuery 31
List each publisher and the average List each publisher and the average price of its booksprice of its books
FORFOR $p $p ININ
distinctdistinct((documentdocument(”bib.xml”)//publisher)(”bib.xml”)//publisher)
LETLET $a := $a :=
avgavg(document(”bib.xml”)/book[(document(”bib.xml”)/book[
publisher =$p]/price)publisher =$p]/price)
RETURNRETURN
<publisher><publisher>
<name> $p/text() </name> ,<name> $p/text() </name> ,
<avgprice> $a </avgprice><avgprice> $a </avgprice>
</publisher></publisher>
SDPL 2001 Notes 8.2: XQuery 32
List the publishers of more than 100 books:List the publishers of more than 100 books:
<big_publishers><big_publishers>
FORFOR $p $p ININ
distinctdistinct((documentdocument(”bib.xml”)//publisher)(”bib.xml”)//publisher)
LETLET $b := document(”bib.xml”)/book[ $b := document(”bib.xml”)/book[
publisher = $p]publisher = $p]
WHEREWHERE countcount($b) > 100($b) > 100
RETURNRETURN $p $p
</big_publishers></big_publishers>
SDPL 2001 Notes 8.2: XQuery 33
Invert the structure of the input document by Invert the structure of the input document by grouping book titles by author names:grouping book titles by author names:
<author_list><author_list>
FORFOR $a $a ININ
distinctdistinct((documentdocument(”bib.xml”)//author)(”bib.xml”)//author)
RETURNRETURN <author> <author>
<name> $a/<name> $a/texttext() </name>() </name>
FORFOR $b $b ININ
documentdocument(”bib.xml”)//book[(”bib.xml”)//book[
author = $a]author = $a]
RETURNRETURN $b/title $b/title
</author></author>
</author_list></author_list>
SDPL 2001 Notes 8.2: XQuery 34
For each book whose price is greater than the average, For each book whose price is greater than the average, return the title of the book and the amount by which the return the title of the book and the amount by which the book’s price exceeds the average pricebook’s price exceeds the average price
<result><result> LETLET $a := $a :=
avgavg((documentdocument(”bib.xml”)//book/price)(”bib.xml”)//book/price) FORFOR $b $b ININ documentdocument(”bib.xml”)//book(”bib.xml”)//book WHEREWHERE $b/price > $a $b/price > $a RETURNRETURN <expensive_book> <expensive_book> $b/title ,$b/title , <price_difference><price_difference> $b/price - $a$b/price - $a </price_difference></price_difference> </expensive_book></expensive_book></result></result>
SDPL 2001 Notes 8.2: XQuery 35
Make an alphabetic list of publishers; within each publisher, Make an alphabetic list of publishers; within each publisher, list books (title & price) in descending price orderlist books (title & price) in descending price order
FORFOR $p $p ININ distinctdistinct((documentdocument(”bib.xml”)//publisher)(”bib.xml”)//publisher)
RETURNRETURN <publisher><publisher>
<name> $p/text() </name><name> $p/text() </name> FORFOR $b $b ININ documentdocument(”bib.xml”)/book[(”bib.xml”)/book[
publisher = $p]publisher = $p] RETURNRETURN
<book> $b/title ,<book> $b/title , $b/price$b/price </book> </book> SORTBYSORTBY(price (price DESCENDINGDESCENDING)) </publisher> </publisher> SORTBYSORTBY(name)(name)
SDPL 2001 Notes 8.2: XQuery 36
Operators in expressions Operators in expressions
Expressions can be constructed using infix and Expressions can be constructed using infix and prefix operators; nested expressions inside prefix operators; nested expressions inside parenthesis can serve as operandsparenthesis can serve as operands
arithmetic and logical operators; collection arithmetic and logical operators; collection operators (UNION, INTERSECT, EXCEPT)operators (UNION, INTERSECT, EXCEPT)
BEFORE and AFTERBEFORE and AFTER– operate on two listsoperate on two lists– return elements in the first list that occur before (after) at return elements in the first list that occur before (after) at
least one element of the second list in document orderleast one element of the second list in document order
SDPL 2001 Notes 8.2: XQuery 37
ExampleExample
Assume: surgical reports with elements Assume: surgical reports with elements procedure, procedure, incision, incision, and and anesthesiaanesthesia
Prepare a ”critical sequence” report consisting of all Prepare a ”critical sequence” report consisting of all elements that occur between the 1st and 2nd incision in the elements that occur between the 1st and 2nd incision in the 1st procedure1st procedure
<critical_sequence><critical_sequence> LETLET $p := //procedure[1] $p := //procedure[1] FORFOR $e $e ININ //* //* AFTERAFTER ($p//incision)[1] ($p//incision)[1] BEFOREBEFORE ($p//incision)[2] ($p//incision)[2] RETURNRETURN $e $e</critical_sequence></critical_sequence>
SDPL 2001 Notes 8.2: XQuery 38
Conditional expressionsConditional expressions
IF-THEN-ELSEIF-THEN-ELSE conditional expressions can be nested and used conditional expressions can be nested and used
wherever a value is expectedwherever a value is expected assume: a library has many holdings (element assume: a library has many holdings (element holdingholding with a with a typetype attribute that identifies its attribute that identifies its type, e.g. book or journal). All holdings have a type, e.g. book or journal). All holdings have a title;title; other nested elements depend on the other nested elements depend on the type of holdingtype of holding
SDPL 2001 Notes 8.2: XQuery 39
Make an ordered list of holdings; For journals, Make an ordered list of holdings; For journals, include the include the editoreditor, and for others, the , and for others, the authorauthor
FORFOR $h $h ININ //holding //holding
RETURNRETURN
<holding><holding>
$h/title,$h/title,
IFIF $h/@type = ”Journal” $h/@type = ”Journal”
THENTHEN $h/editor $h/editor
ELSEELSE $h/author $h/author
</holding> </holding> SORTBYSORTBY (title) (title)
SDPL 2001 Notes 8.2: XQuery 40
QuantifiersQuantifiers
Sometimes necessary to test for existence of Sometimes necessary to test for existence of some element that satisfies a condition, or to some element that satisfies a condition, or to determine whether all elements in some collection determine whether all elements in some collection satisfy a conditionsatisfy a condition
-> existential and universal quantifiers -> existential and universal quantifiers
SDPL 2001 Notes 8.2: XQuery 41
Find titles of books in which both sailing and Find titles of books in which both sailing and windsurfing are mentioned in the same paragraphwindsurfing are mentioned in the same paragraph
FORFOR $b $b ININ //book //book
WHERE SOMEWHERE SOME $p $p ININ $b//para $b//para SATISFIESSATISFIES
contains($p, ”sailing”)contains($p, ”sailing”)
ANDAND contains($p, ”windsurfing”) contains($p, ”windsurfing”)
RETURNRETURN $b/title $b/title
SDPL 2001 Notes 8.2: XQuery 42
Find titles of books in which sailing is Find titles of books in which sailing is mentioned in every paragraphmentioned in every paragraph
FORFOR $b $b ININ //book //book
WHERE EVERYWHERE EVERY $p $p ININ $b//para $b//para SATISFIESSATISFIES
contains($p, ”sailing”)contains($p, ”sailing”)
RETURNRETURN $b/title $b/title
SDPL 2001 Notes 8.2: XQuery 43
FilteringFiltering
Function Function filter(Forest1, Forest2) filter(Forest1, Forest2) – in XQuery core function libraryin XQuery core function library
returns copies of some of the nodes in Forest1 returns copies of some of the nodes in Forest1 represented by the first operandrepresented by the first operand– order and hierarchy are preservedorder and hierarchy are preserved
nodes that are copied:nodes that are copied:– nodes that are present at any level in Forest1 and nodes that are present at any level in Forest1 and
are also top-level nodes in the Forest2are also top-level nodes in the Forest2
SDPL 2001 Notes 8.2: XQuery 44
Action of FILTER on a hierarchyAction of FILTER on a hierarchy
Filter Filter prunes from Forest1 any nodes that are prunes from Forest1 any nodes that are not top-level nodes in Forest2 not top-level nodes in Forest2
SDPL 2001 Notes 8.2: XQuery 45
Table of contents for document ”cookbook.xml”, Table of contents for document ”cookbook.xml”, containing nested sections and their titlecontaining nested sections and their title
LETLET $b := $b := documentdocument(”cookbook.xml”)(”cookbook.xml”)
RETURNRETURN
<toc><toc>
filterfilter($b, $b//section | ($b, $b//section |
$b//section/title | $b//section/title |
$b//section/title/text() ) $b//section/title/text() )
</toc></toc>
SDPL 2001 Notes 8.2: XQuery 46
Other built-in functions Other built-in functions
A core library of built-in functionsA core library of built-in functions documentdocument: returns the root node of a named : returns the root node of a named
document (like in XSLT)document (like in XSLT) all functions of the XPath core function library all functions of the XPath core function library all the aggregation functions of SQL all the aggregation functions of SQL
– avg, sum, count, max, min…avg, sum, count, max, min…
distinctdistinct: eliminates nodes with duplicate value : eliminates nodes with duplicate value emptyempty: returns TRUE if and only if its argument is : returns TRUE if and only if its argument is
an empty listan empty list
SDPL 2001 Notes 8.2: XQuery 47
User-defined functions User-defined functions
Users are allowed to define own functionsUsers are allowed to define own functions each function definition must each function definition must
– declare the datatypes of its parameters and resultdeclare the datatypes of its parameters and result– provide an expression that defines how the result of the provide an expression that defines how the result of the
function is computed from its parametersfunction is computed from its parameters
when a function is invoked, its arguments must be when a function is invoked, its arguments must be valid instances of the declared parameter typesvalid instances of the declared parameter types
the result must also be a valid instance of its the result must also be a valid instance of its declared typedeclared type
SDPL 2001 Notes 8.2: XQuery 48
Functions Functions
A function may be defined recursivelyA function may be defined recursively– the function is referenced in its own definitionthe function is referenced in its own definition
Example: find the maximum depth of the document Example: find the maximum depth of the document named ”partlist.xml”named ”partlist.xml”
FUNCTIONFUNCTION depth( depth(ELEMENTELEMENT $e) $e) RETURNSRETURNS xsd:integer xsd:integer{ { IFIF empty($e/*) empty($e/*) THENTHEN 1 1 ELSEELSE maxmax(depth($e/*)) + 1(depth($e/*)) + 1}}
callcall: depth(: depth(documentdocument(”partlist.xml”))(”partlist.xml”))
SDPL 2001 Notes 8.2: XQuery 49
Querying relational dataQuerying relational data
A lot of data is stored in relational databasesA lot of data is stored in relational databases an XML query language should be able to access an XML query language should be able to access
this datathis data Example: suppliers and partsExample: suppliers and parts
– Table S: supplier numbers (sno) and names (sname)Table S: supplier numbers (sno) and names (sname)– Table P: part numbers (pno) and descriptions (descrip)Table P: part numbers (pno) and descriptions (descrip)– Table SP: relationships between suppliers and the Table SP: relationships between suppliers and the
parts they supply, including the price (price) of each parts they supply, including the price (price) of each part from each supplierpart from each supplier
SDPL 2001 Notes 8.2: XQuery 50
One possible XML representation of One possible XML representation of relational datarelational data
SDPL 2001 Notes 8.2: XQuery 51
SQL vs. XQuery SQL vs. XQuery
SQL:SQL:
XQuery:XQuery:
SELECTSELECT pno pnoFROMFROM p pWHEREWHERE descrip descrip LIKELIKE ’Gear’ ’Gear’ORDER BYORDER BY pno; pno;
FORFOR $p $p ININ documentdocument(”p.xml”)//p_tuple(”p.xml”)//p_tupleWHEREWHERE contains($p/descrip, ”Gear”) contains($p/descrip, ”Gear”)RETURNRETURN $p/pno $p/pno SORTBYSORTBY(.)(.)
SDPL 2001 Notes 8.2: XQuery 52
GroupingGrouping
Many relational queries involve forming data into Many relational queries involve forming data into groups and applying some aggregation function groups and applying some aggregation function such as such as countcount or or avgavg to each group to each group
in SQL: GROUP BY and HAVING clausesin SQL: GROUP BY and HAVING clauses Example: Find the part number and average Example: Find the part number and average
price, for parts that have at least 3 suppliersprice, for parts that have at least 3 suppliers
SDPL 2001 Notes 8.2: XQuery 53
Grouping: SQLGrouping: SQL
SELECTSELECT pno, pno, avgavg(price) (price) ASAS avgprice avgprice
FROMFROM sp sp
GROUP BY GROUP BY pnopno
HAVING countHAVING count(*) >= 3(*) >= 3
ORDER BYORDER BY pno; pno;
SDPL 2001 Notes 8.2: XQuery 54
Grouping: XQueryGrouping: XQuery
FORFOR $pn $pn IN distinctIN distinct((documentdocument(”sp.xml”)//pno)(”sp.xml”)//pno)
LETLET $sp := document(”sp.xml”)//sp_tuple[ $sp := document(”sp.xml”)//sp_tuple[
pno = $pn]pno = $pn]
WHERE countWHERE count($sp) >= 3($sp) >= 3
RETURNRETURN
<well_supplied_item><well_supplied_item>
$pn,$pn,
<avgprice> <avgprice> avgavg($sp/price) </avgprice)($sp/price) </avgprice)
<well_supplied_item> <well_supplied_item>
SORTBYSORTBY(pno)(pno)
SDPL 2001 Notes 8.2: XQuery 55
JoinsJoins
Example: Return a ”flat” list of supplier names and their Example: Return a ”flat” list of supplier names and their part descriptions, in alphabetic orderpart descriptions, in alphabetic order
FORFOR $sp $sp ININ documentdocument(”sp.xml”)//sp_tuple,(”sp.xml”)//sp_tuple, $p $p ININ documentdocument(”p.xml”)//p_tuple[(”p.xml”)//p_tuple[
pno = $sp/pno],pno = $sp/pno], $s $s IN documentIN document(”s.xml”)//s_tuple[(”s.xml”)//s_tuple[
sno = $sp/sno]sno = $sp/sno]RETURNRETURN <sp_pair> <sp_pair>
$s/sname ,$s/sname , $p/descrip$p/descrip
<sp_pair> <sp_pair> SORTBYSORTBY (sname, descrip) (sname, descrip)