120 likes | 239 Views
XQuery. Leah Andrews. Overview. Queries data stored in XML trees Declarative High-level Functional (no side effects) Strongly typed Nodes Atomic values (integers, strings, booleans). Origins. Became a W3 standard in January, 2007
E N D
XQuery Leah Andrews
Overview • Queries data stored in XML trees • Declarative • High-level • Functional (no side effects) • Strongly typed • Nodes • Atomic values (integers, strings, booleans)
Origins • Became a W3 standard in January, 2007 • Developed by Jonathan Robie, Don Chamberlin and Daniela Florescu • Compatible with other W3 standards • XML (namespaces, Schema), XSLT, XPath
XQuery v. XSLT • Overlapping capabilities • Share a data model, type system, function library and XPath
XQuery > XSLT • Better usability • More concise • Other languages can be embedded • More Orthogonal • Can be expressed in XML syntax (XQueryX)
XSLT > XQuery • Better for small-scale changes to XML documents • Widespread use • More flexible for transformations
XQuery does NOT support: • Full text search capacity • Updating XML documents or databases • Dynamic typing • Polymorphism
FLWOR • FOR • LET • WHERE • ORDER BY • RETURN
<?xml version="1.0" encoding="ISO-8859-1"?> <class> <students> <student name="Leah"> <studentId>1</studentId> <laptop>MacBook</laptop> <favoritecolor>olive</favoritecolor> </student> <student name="Ian"> <studentId>2</studentId> <laptop>WinBook</laptop> <favoritecolor>orange</favoritecolor> </student> <student name="Kyle"> <studentId>3</studentId> <laptop>Toshiba</laptop> <favoritecolor>orange</favoritecolor> </student> <student name="Chris"> <studentId>4</studentId> <laptop>Gateway</laptop> <favoritecolor>green</favoritecolor> </student> <student name="Stacy"> <studentId>5</studentId> <laptop>Asus</laptop> <favoritecolor>rainbow</favoritecolor> </student> <student name="Tom"> <studentId>6</studentId> <laptop>ZT</laptop> <favoritecolor>black</favoritecolor> </student> <student name="Brad"> <studentId>7</studentId> <laptop>IMB</laptop> <favoritecolor>unknown</favoritecolor> </student> </students> <professors> <professor name="Axel"> <id>8</id> <laptop>MacBook</laptop> <favoritecolor>German</favoritecolor> </professor> </professors> </class>
xquery version "1.0"; <html> <body> <p>All students</p> <ul> { for $x in doc("students.xml")/class/students order by $x/studentId return <li> { $x } </li> } </ul> </body> </html>
xquery version "1.0"; <html> <body> <p>Students with an id less than 5</p> <ul> { for $x in doc("students.xml")/class/students where $x/studentId<5 order by $x/studentId return <li> {data($x/@name)} </li> } </ul> </body> </html>
xquery version "1.0"; <html> <body> <p>Favorite (student) colors</p> <ul> { for $x in doc("students.xml")/class/students/favoritecolor return <li> {data($x)} </li> } </ul> </body> </html>