introduction to xquery bun yue professor, cs/cis uhcl
TRANSCRIPT
![Page 1: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/1.jpg)
Introduction to XQuery
Bun YueProfessor, CS/CISUHCL
![Page 2: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/2.jpg)
W3C Recommendations http://www.w3.org/TR/xquery/: W3C XQuery
http://www.w3.org/TR/xmlquery-use-cases: XQuery use cases.
http://www.w3.org/TR/xquery-operators/: XQuery and XPath functions.
http://www.w3.org/TR/xpath-datamodel/: XQuery 1.0 and XPath 2.0 Data Model.
http://www.w3.org/TR/xpath20/: XPath 2.0. http://www.w3.org/TR/xmlschema-1/: XML Schema
Part 1: Structures. http://www.w3.org/TR/xmlschema-2/: XML Schema
Part 2: datatypes.
![Page 3: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/3.jpg)
Introduction
XQuery is designed for effectively query and retrieve information from a diversified XML sources.
The XML sources can be one or more XML documents.
XQuery is derived from Quilt, and has borrowed features from XPath, XQL, SQL, etc.
![Page 4: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/4.jpg)
Introduction It is a functional language where a
query is an expression. There are three faces of the XQuery
languages: A "surface" syntax that programmers
may probably use. An XML-based syntax that machine may
probably use (XQueryX). A formal semantic that XQuery engine
implementators use.
![Page 5: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/5.jpg)
Introduction. XQuery 1.0 extends XPath 2.0. The type system of XQuery is based
on XML Schema. A limitation of XQuery:
No update or insert. The basic building block of XQuery is
expressions. (In this sense, like SQL, XQuery is not a full programming language.)
![Page 6: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/6.jpg)
Comparing to SQL
Relational DB: SQL
XML DB: XQuery
Basic units relations collections
Records tuples or rows of schema
documents of same schema
Schema Relational Schema
DTD, XML Schema
Query results Relations: unordered list of rows
Ordered sequences of nodes.
![Page 7: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/7.jpg)
Review of XPath 2.0 The value of an expression is a sequence,
which is an ordered list of items. An item can be a node or of atomic value. There are 7 node types:
Document Element Attribute Comment Text Processing Instruction Namespace
![Page 8: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/8.jpg)
XQueryXFor doc("census.xml")//person[@job="Athlete"]the corresponding XQueryX can be:
<?xml version="1.0"?><q:query xmlns:q="http://www.w3.org/2001/06/xqueryx"> <q:step q:axis="descendant-or-self"> <q:function q:name="document"> <q:constant q:datatype="xs:string">census.xml</q:constant> </q:function> <q:predicatedExpr> <q:identifier>person</q:identifier> <q:predicate> <q:function q:name="equals"> <q:step q:axis="attribute"> <q:identifier>job</q:identifier>
</q:step> <q:constant q:datatype="xs:string">Athlete</q:constant> </q:function> </q:predicate>
</q:predicatedExpr> </q:step></q:query>
![Page 9: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/9.jpg)
Data Types
XQuery is strongly typed. XQuery types are based on
XML Schema: using the namespace prefix xs and url: http://www.w3.org/2001/XMLSchema.
XPath functions and operators: using the namespace prefix xdt and url: http://www.w3.org/2004/07/xpath-datatypes
![Page 10: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/10.jpg)
Types
![Page 11: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/11.jpg)
Types
xdt:untyped is used to denote element nodes not yet validated.
xdt:untypedAtomic is used to denote atomic types that has not been assigned a more specific type.
![Page 12: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/12.jpg)
Query
A query in XQuery is an expression for reading XML documents or fragments
and returning a sequence of well-formed XML
fragments
Everything in XQuery is an expression that is evaluated to a value.
![Page 13: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/13.jpg)
Query expressions Some common forms of XQuery
expressions are (these appear in most tutorials): path expressions element constructors FLWR or FLOWR (pronounced as "flower")
expressions list expressions conditional expressions quantified expressions datatype expressions
![Page 14: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/14.jpg)
More Queries Examples of other expressions
include: primary expressions sequence expressions arithmetic expressions logical expressions comparison expressions sorting expressions validate expressions
![Page 15: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/15.jpg)
Comments
XQuery comments are embedded within (: and :).
![Page 16: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/16.jpg)
Functions
Supports a collection of about 200 built-in operators and functions to be used within expressions.
Input functions in XQuery include doc() and collection(). They are used to identify the sources of the XML documents.
![Page 17: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/17.jpg)
Input Functions
Input functions: doc() collection().
![Page 18: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/18.jpg)
Prolog
XQuery may have prologs for declarations. Examples: Variable declarations Function declarations Base-URI declarations Version declarations Module import …
![Page 19: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/19.jpg)
Variable Declarations
Format: declare variable $name = expression;
E.g.
declare variable $a := doc("census.xml")//person ;
![Page 20: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/20.jpg)
Path Expressions
XQuery 1.0 is a superset of XPath 2.0. An XPath expression is also an
XQuery expression
![Page 21: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/21.jpg)
Editix
Use “View > Windows > XQuery Builder”
For XQ files, use “XSLT/XQuery > Transform using an XQuery Request…” Specify source xq file, xml file and output
file. Use .xml extension. If you use .txt
extension, only text node contents are output.
![Page 22: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/22.jpg)
Examples
declare base-uri "whatever-path";doc("bib.xml")/*
Return basically bib.xml.
![Page 23: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/23.jpg)
Example
doc("bib.xml")//*
Return many nodes (in a sequence).
Results are not well-formed.
![Page 24: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/24.jpg)
Examples
doc("bib.xml")//book[@year]
count(doc("census.xml")//person)
![Page 25: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/25.jpg)
Element Constructors
Element constructors can be used to construct XML elements.
If the name, attributes, and content of the element are all constants, the element constructor is based on standard XML notation and is called a direct element constructor (W3C).
![Page 26: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/26.jpg)
Example
The XQuery<authors><author>Bun Yue</author></authors>returns<authors><author>Bun Yue</author></authors>
![Page 27: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/27.jpg)
Element Constructors
XQuery expressions can be embedded in the direct element constructors within a pair of curly braces, {}.
For the characters '{' and '}', use '{{' and '}}' respectively.
XQuery expressions may be separated by commas.
![Page 28: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/28.jpg)
Example
<authors><author>Bun Yue</author>{ doc("bib.xml")//author }</authors>
Adds Bun Yue to the authors of bib.xml.
![Page 29: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/29.jpg)
Computed Constructors Computed constructors can also be used to
declare nodes: Use the keywords element, attribute, document,
text, processing-instruction, comment, or namespace to declare the type of the nodes.
Specify the node names for those node types with names (element, attribute, processing instruction, and namespace nodes)
Use a pair of braces to define the content expressions.
Note the use of commas to separate expressions in the context.
![Page 30: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/30.jpg)
Example (from W3C)
element book { attribute isbn {"isbn-0060229357" }, element title { "Harold and the Purple
Crayon"}, element author { element first { "Crockett" },
element last {"Johnson" } }}
![Page 31: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/31.jpg)
Example (result)
<book isbn="isbn-0060229357"> <title>Harold and the Purple Crayon</title> <author> <first>Crockett</first> <last>Johnson</last> </author>
</book>
![Page 32: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/32.jpg)
Dynamic Element Names
Computed expressions can be used to create elements with dynamic names.
![Page 33: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/33.jpg)
Example
<result>{ for $author in doc("bib.xml")//author return element {$author/last/text()} { $author/first }}</result>
![Page 34: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/34.jpg)
Example Result<?xml version="1.0" encoding="UTF-8"?><result> <Stevens> <first>W.</first> </Stevens> <Stevens> <first>W.</first> </Stevens> <Abiteboul> <first>Serge</first> </Abiteboul> <Buneman> <first>Peter</first> </Buneman> <Suciu> <first>Dan</first> </Suciu></result>
![Page 35: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/35.jpg)
Example Note that <first> is a child element. See
the difference of:<result>{ for $author in doc("bib.xml")//author return element {$author/last/text()} { $author/first/text() }}</result>
![Page 36: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/36.jpg)
Example
This example may also result in a runtime error (as the value of <last> may not be suitable for a QName.
![Page 37: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/37.jpg)
FLWOR expressions
FLWOR expressions are one of the most important constructs in XQuery.
You may compare with the SELECT statement of SQL.
![Page 38: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/38.jpg)
FLWOR (W3C)[42] FLWORExpr ::= (ForClause | LetClause)+ WhereClause?
OrderByClause? "return" ExprSingle[43] ForClause ::= "for" "$" VarName TypeDeclaration?
PositionalVar? "in" ExprSingle ("," "$" VarName TypeDeclaration? PositionalVar? "in" ExprSingle)*
[45] LetClause ::= "let" "$" VarName TypeDeclaration? ":=" ExprSingle ("," "$" VarName TypeDeclaration? ":=" ExprSingle)*
[123] TypeDeclaration ::= "as" SequenceType[44] PositionalVar ::= "at" "$" VarName[46] WhereClause ::= "where" Expr[47] OrderByClause ::= ("order" "by" | "stable" "order" "by")
OrderSpecList [48] OrderSpecList ::= OrderSpec ("," OrderSpec)*[49] OrderSpec ::= ExprSingle OrderModifier[50] OrderModifier ::= ("ascending" | "descending")? (("empty"
"greatest") | ("empty" "least"))? ("collation" StringLiteral)?
![Page 39: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/39.jpg)
FLWOR FLWOR expressions allow:
For: Iteration through items in XPath 2.0 sequences. Create a tuple stream where each tuple contains a distinct binding for each variable to a distinct value.
Let: Variables binding Where: Predicate application for inclusion in the
iteration. Order by: Ordering data set for the iteration. Return: Constructing new result for returning.
![Page 40: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/40.jpg)
For and Let
The for and let clauses produces a tuple stream.
A tuple consists of one or more bound variables.
A variable begins with the prefix $. A bound variable is one that has been
assigned a value.
![Page 41: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/41.jpg)
Example
declare base-uri “whatever”;let $a := doc("bib.xml")//authorreturn<authors> { $a }</authors>
![Page 42: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/42.jpg)
Example Results<?xml version="1.0" encoding="UTF-8"?><authors> <author> <last>Stevens</last> <first>W.</first> </author> <author> <last>Stevens</last> <first>W.</first> </author>…</authors>
![Page 43: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/43.jpg)
Example Note
In this example: The tuple stream is composed of only
one tuple. The variable $b in this tuple is bound to
the node sequence of 5 <author> nodes.
![Page 44: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/44.jpg)
Example
for $a in doc("bib.xml")//authorreturn<authors> { $a }</authors>
![Page 45: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/45.jpg)
Example Result<?xml version="1.0" encoding="UTF-8"?><authors> <author> <last>Stevens</last> <first>W.</first> </author></authors><authors> <author> <last>Stevens</last> <first>W.</first> </author></authors>…</authors>
![Page 46: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/46.jpg)
Example Notes
In this example: The tuple stream is composed of only
five tuples. The variable $b in this tuple is bound to
one <author> node at a time.
![Page 47: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/47.jpg)
Example
for $a in doc("bib.xml")//author, $b in doc("bib.xml")//authorreturn <count/>
![Page 48: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/48.jpg)
Example Result
<?xml version="1.0" encoding="UTF-8"?>
<count/><count/><count/><count/>… (: 25 counts :)
![Page 49: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/49.jpg)
Example Note The tuple stream is composed of only 25 tuples. The 25 tuples are:
($a: <author><last>Stevens</last><first>W.</first></author>, $b: <author><last>Stevens</last><first>W.</first></author>)
($a: <author><last>Stevens</last><first>W.</first></author>, $b: <author><last>Stevens</last><first>W.</first></author>)
($a: <author><last>Stevens</last><first>W.</first></author>, $b: <author><last>Abiteboul</last><first>Serge</first></author>)
…
![Page 50: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/50.jpg)
Example
for $a in doc("bib.xml")//author, $b in $a/lastreturn <count />
![Page 51: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/51.jpg)
Example Result
<?xml version="1.0" encoding="UTF-8"?>
<count/><count/><count/><count/><count/>
![Page 52: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/52.jpg)
Example Note The tuple stream is composed of only 5 tuples. The 5 tuples are:
($a: <author><last>Stevens</last><first>W.</first></author>, $b: <last>Stevens</last)
($a: <author><last>Stevens</last><first>W.</first></author>, $b: <last>Stevens</last)
($a: <author><last>Abiteboul</last><first>Serge</first></author>, $b: <last>Abiteboul</last)
…
![Page 53: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/53.jpg)
Example
for $a in doc("bib.xml")//author, $b in doc("bib.xml")//authorwhere $a = $breturn <result><alast>{ $a/last/text()
}</alast><blast>{ $b/last/text() }</blast></result>
![Page 54: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/54.jpg)
Example Result<?xml version="1.0" encoding="UTF-8"?><result> <alast>Stevens</alast> <blast>Stevens</blast></result>… (: three more times. :)<result> <alast>Abiteboul</alast> <blast>Abiteboul</blast></result><result> <alast>Buneman</alast> <blast>Buneman</blast></result><result> <alast>Suciu</alast> <blast>Suciu</blast></result>
![Page 55: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/55.jpg)
Example Note The tuple stream is composed of only 7 tuples. The 7 tuples are:
($a: <author><last>Stevens</last><first>W.</first></author>, $b: <author><last>Stevens</last><first>W.</first></author>) (: 4 times)
($a: <author><last>Abiteboul</last><first>Serge</first></author>, $b: <author><last>Abiteboul</last><first>Serge</first></author>)
…
![Page 56: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/56.jpg)
Example
<figlist> {for $f in doc("tree-data.xml")//figure return <diagram> { $f/@* } { $f/title } </diagram>}</figlist>
![Page 57: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/57.jpg)
Example Result<?xml version="1.0" encoding="UTF-8"?><figlist> <diagram height="400" width="400"> <title>Traditional client/server architecture</title> </diagram> <diagram height="200" width="500"> <title>Graph representations of structures</title> </diagram> <diagram height="250" width="400"> <title>Examples of Relations</title> </diagram></figlist>
![Page 58: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/58.jpg)
Example Note There are three tuples in the tuple stream
of the for clause. Each tuple has one variable: $f, which is bounded to each of the three <figure> elements in the input xml contents respectively.
{ $f/@* } returns the attributes of the original <figure> elements, which will be put as attributes of the output <figure> element.
![Page 59: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/59.jpg)
Example
<authors> { fn:string-join(for $a in doc("tree-
data.xml")//author return $a/text(), ", ") }
</authors>
![Page 60: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/60.jpg)
Example Result
<?xml version="1.0" encoding="UTF-8"?>
<authors>Serge Abiteboul, Peter Buneman, Dan Suciu</authors>
![Page 61: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/61.jpg)
Example Note
fn:string-join takes two arguments: A sequence of string, and A string join separator
![Page 62: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/62.jpg)
Example
<book> {for $f in doc("tree-data.xml")//figure return <figure> { attribute size { $f/@width *
$f/@height } } </figure>}</book>
![Page 63: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/63.jpg)
Example Result
<?xml version="1.0" encoding="UTF-8"?>
<book> <figure size="160000"/> <figure size="100000"/> <figure size="100000"/></book>
![Page 64: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/64.jpg)
Example
<book> { for $f in doc("tree-data.xml")//figure let $size := $f/@width * $f/@height order by $size return <figure> { attribute size { $size } } </figure>}</book>
![Page 65: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/65.jpg)
Example Result
<?xml version="1.0" encoding="UTF-8"?>
<book> <figure size="100000"/> <figure size="100000"/> <figure size="160000"/></book>
![Page 66: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/66.jpg)
Exercise #1 Use bib.xml, Show all books published by Addison
Wesley.<bib>
<book> <title>TCP/IP Illustrated</title> <author><last>Stevens</last><first>W.</first></author> </book> <book> <title>Advanced Programming in the Unix environment</title> <author><last>Stevens</last><first>W.</first></author> </book></bib>
![Page 67: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/67.jpg)
Exercise #2 All books by Addison-Wesley using different
format:<bib>
<book author="W. Stevens"> <name>TCP/IP Illustrated</name> </book> <book author="W. Stevens"> <name>Advanced Programming in the Unix environment</name> </book>
</bib>
![Page 68: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/68.jpg)
Exercise #3
All books written by W. Stevens ordered by years:
<result> <book-title>Advanced Programming
in the Unix environment</book-title> <book-title>TCP/IP Illustrated</book-
title></result>
![Page 69: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/69.jpg)
Exercise #4 All books written by W. Stevens
ordered by years in descending order:<result> <book-title>TCP/IP Illustrated</book-
title> <book-title>Advanced Programming
in the Unix environment</book-title></result>
![Page 70: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/70.jpg)
Exercise #5 Use ft2.xml, return every <person> with its
<first> and <last> child elements. Add a child element <numEmail> to include the number of email addresses.
<result> <person> <first>Boris</first> <last>Becker</last> <numEmail>2</numEmail> </person>…
</result>
![Page 71: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/71.jpg)
Exercise #6 Return all <person> elements with all attributes. The body of the
<person> element should be the name of the person in the format of first name and then last name. For ft2.xml, it returns:
<result> <person ssn="s123456789" gender="M" luckynumber="7">Boris Becker</person> <person ssn="s111222333" gender="F" luckynumber="6">Valerie Becker</person> <person ssn="s123123123" gender="M" luckynumber="4">Chris Becker</person> <person ssn="s222333444" gender="F">Julie Becker</person> <person ssn="s555987323" gender="M">John Becker</person> <person ssn="s887667545" gender="F">Mary Becker</person>
</result>
![Page 72: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/72.jpg)
Exercise #7 Return all pairs of <first> elements of persons with the same last
name, not including pairing with oneself. Each pair of result is embedded in an element with the last name of the persons as the element name. For ft2.xml, it returns:
<result> <Becker><first>Boris</first><first>Valerie</first></Becker> <Becker><first>Boris</first><first>Chris</first></Becker> <Becker><first>Boris</first><first>Julie</first></Becker> <Becker><first>Boris</first><first>John</first></Becker> <Becker><first>Boris</first><first>Mary</first></Becker> <Becker><first>Valerie</first><first>Boris</first></Becker>
…</result>
![Page 73: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/73.jpg)
Exercise #8 Convert all text nodes to <text /> and all
elements with name x to <element name="x" />. For ft2.xml, it returns:
<result> <element name="familytree"/> <text/> <text/> <element name="meta"/>
…</result>
![Page 74: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/74.jpg)
Function Declarations XQuery allows user-defined functions in the
prolog.[26] FunctionDecl ::= "declare" "function"
QName "(" ParamList? ")" ("as" SequenceType)? (EnclosedExpr | "external")
[27] ParamList ::= Param ("," Param)*[28] Param ::= "$" QName
TypeDeclaration?[118] TypeDeclaration ::= "as"
SequenceType
![Page 75: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/75.jpg)
Example: factorial($i)
declare function local:factorial($i as xs:integer) as xs:integer
{ if ($i < 0) then 0 else if ($i = 0) then 1 else $i * local:factorial($i - 1)};local:factorial(6)
![Page 76: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/76.jpg)
Functions There is a ; after the function declaration. The namespace prefix local is used for user-
defined functions. XQuery predefines the namespace prefix local to the namespace http://www.w3.org/2004/07/xquery-local-functions, and reserves this namespace for use in defining local functions.
The types of the arguments and return values should be sequence types.
![Page 77: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/77.jpg)
Types
Sequence type can be: empty(), or ItemType OccurrenceIndicator?
OccurrenceIndicator can be +, ? or *. Item type can be:
item() atomic type, or kind test.
![Page 78: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/78.jpg)
Kind Tests
Important kind tests include node() text() comment() processing-instruction(): with optional
name argument. element test attribute test
![Page 79: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/79.jpg)
Element Tests
Example of element tests are: element(*) element(familytree) element(man, personType)
![Page 80: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/80.jpg)
Functions
Writing XQuery functions: Functional programming. Many are recursive in nature. Beware of types of parameters and
return values.
![Page 81: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/81.jpg)
Example from W3Cdeclare function local:depth($e as node()) as xs:integer{ (: A node with no children has depth 1 :) (: Otherwise, add 1 to max depth of children :) if (fn:empty($e/*)) then 1 else fn:max(for $c in $e/* return local:depth($c)) + 1};<result>{ local:depth(doc("ft2.xml"))}</result>
![Page 82: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/82.jpg)
Exercise #9
Write an XQuery function to count the number of elements in an element node (including itself). Try to use a recursive solution.
![Page 83: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/83.jpg)
Exercise #10
For XML document such as ft2.xml, write a function that returns all child person nodes with parent of social security number $ssn.
![Page 84: Introduction to XQuery Bun Yue Professor, CS/CIS UHCL](https://reader031.vdocument.in/reader031/viewer/2022012916/56649eaa5503460f94baf972/html5/thumbnails/84.jpg)
Questions