Download presentation
Presentation is loading. Please wait.
1
Introduction to XQuery and eXist Week 17 DSA
2
DSA - XQuery2 XPath. Hierarchical file systems have been navigable with path expression since Unix –/abc/cde/../../efg i.e. ? Problem –Only child, parent and root can be accessed in one step –Only one node addressable
3
DSA - XQuery3 XPath.. Solution – extend the path language –select multiple items /abc/cde/../../efg/* //x - all x nodes anywhere in the tree –select a subset of items by appending predicate (filter) //x[@size>1000] –functions in predicate //x[ends-with(@name,’.jpeg’)] –multiple filters //x[@owner=‘fred’]//images[ends-with(@name,’.jpeg’)] –select items by position /abc[2] short for /abc[position() = 2]
4
DSA - XQuery4 XPath … Core data type in XPath 2.0 is a sequence of items –Items are atomic values or XML elements –Sequence is flat deep-equal('fred',('fred')) is true deep-equal((‘fred’, ‘fred’), (‘fred’)) is false deep-equal(('fred', ('bill','joe')),('fred', 'bill','joe')) is true –() is the empty sequence count((‘fred’,() )) is 1 Equality between sequences defined as non-empty intersection (‘fred’,’joe’) = ‘joe’ is true (‘fred’,’joe’) = (‘joe’,’bill’) is true (‘fred’,’joe’) eq ‘joe’ is false
5
DSA - XQuery5 XQuery. Problem –Need to be able to construct a tree as well as select nodes in an existing tree –Need for more complex searches a la SQL Solution 1 – XML addition to existing languages –PHP + Simple XML –XML structures not compatible with language Solution 2 – XSLT –Push (declarative, pattern-matching) or pull processing –XML- based –Two versions 1.0 most common, in Browsers, Xalan 2.0 e.g. Saxon Solution 2 – XQuery –Functional –Pull only –Non XML XQuery 1.0 and XSLT 2.0 share the same XPath 2.0 data model and function library
6
DSA - XQuery6 eXist Native XML Database Open source Wolfgang Meier is the chief architect Written in Java Deployable in different ways –Embedded in a Java application –Part of a Cocoon pipeline –As web application in Apache/Tomcat –With embedded Jetty HTTPserver (as on stocks) Multiple Interfaces –REST – to Java servlet –SOAP –XML:RPC
7
DSA - XQuery7 Native XML database Well-formed XML documents can be added to the database They are stored in an efficient, searchable B+ tree structure Documents (files) are organised into collections in a filestore Non-XML resources (XQuery, CSS, JPEG..), etc can be stored as binary
8
DSA - XQuery8 Distillery Example My Sample Distillery dataSample Distillery Use the eXist demo servereXist demo server Login as guest/guest Create a folder Upload XML files Test queries in the Sandbox Create XQuery scripts in a text editor Upload XQuery script Execute
9
DSA - XQuery9 Simple Xquery – list1.xql for $d in //Distillery return {$d/Name} {$d/WhiskyDescription}
10
DSA - XQuery10 Executing an XQuery eXist DB a.xql XQuery Engine parameters html Client Browser eXist: Server Get a.xql parameters servlet fetch a.xql render User clicks link
11
DSA - XQuery11 Simple XQuery – list2.xql xquery version "1.0"; Name Address {for $w in //Distillery return {data($w/Name)} {data($w/Address)} }
12
DSA - XQuery12 xquery version "1.0"; Name Address { for $w in //Distillery return {data($w/Name)} {data($w/Address)} } XQuery explained
13
DSA - XQuery13 XQuery.. Nesting XML and XQuery –XML to XQuery { … } –XQuery to XML … Must be well-formed XML – single root - and handy Constructed structures – XML element or element Position { attribute latitude ’51’, attribute longitude ‘2.5’ } –Sequence ('fred',, 5)
14
DSA - XQuery14 XQuery.. control constructs are expressions and hence composable –if (cond) then exp else exp –for..let.. where.. order by.. return – FLWOR –function call –variable value ($d) ‘Atomisation’ sometimes needed in output –Element to characters –$d/Name Glenfiddich –data($d/Name) Glenfiddich
15
DSA - XQuery15 XQuery … Functions and Modules –Typed arguments and return –Recursion –XPath functions –eXist functions Database management HTTP interface.. Functional language –let $x := 5 binds the value 5 to the variable $x –Can’t write iterative code – what’s the output? let $y := 1 return for $x in (1 to 5) let $y := $y * 2 return $y
16
DSA - XQuery16 XQuery FLWOR expression FLWOR for $x in sequence let $a := expression, $b := expression where condition order by $var return expression –Returns a Sequence of nodes Compare with SQL select columns from tables where condition order by –Returns a Relation (table)
17
DSA - XQuery17 Search Query Enter a name or part name Match against the Distillery name List the matches
18
DSA - XQuery18 List 3 let $name := request:request-parameter("name","") return Name Address {for $d in //Distillery[contains(Name,$name)] return {data($d/Name)} {data($d/Address)} }
19
DSA - XQuery19 An XQuery sticky form The script here has several shifts between XML to XQuery Whole interface in one script –Equivalent to PHP + MySQL.
20
DSA - XQuery20 let $name := request:request-parameter("name","") return Enter Name or part of Name Name Address { for $d in //Distillery[contains(Name,$name)] return {data($d/Name)} {data($d/Address)} } list4.xql
21
DSA - XQuery21 Generate a kml overlay Output is kml Need to specify the mime type so that the browser will link to Google Earth (if set up correctly) Or create a Google Map link: http://maps.google.com/maps?q=http://demo.exist- db.org/rest//db/chriswallace/whisky/distkml.xqlhttp://maps.google.com/maps?q=http://demo.exist- db.org/rest//db/chriswallace/whisky/distkml.xql
22
DSA - XQuery22 Kml declare option exist:serialize "method=xhtml indent=yes media-type=application/vnd.google-earth.kml+xml"; Distilleries of Scotland 2 {for $d in //Distillery return {data($d/Name)} {data($d/WhiskyDescription)} {data($d/Position/@longitude)}, {data($d/Position/@latitude)},0 } distkml.xql
23
DSA - XQuery23 Tools for XQuery on eXist Use a text editor (Notepad, PFE32 or Dreamweaver) Use the admin interface with guest/guest login to create a directory and load all the files – including the XQuery files To run a script, just click on the script in the directory listing – this will run the script (but with no parameters – you can add these in the Browser This will show you the URL to execute it – you can then use this to link from any HTML outside the eXist database. Use the sandbox to test small bits of XQuery and XPath
24
DSA - XQuery24 eXist on your own machine Simple install from the exist-db site Use the Java client to –load files –move, copy, rename files –edit files in situ (but no Save-as) –execute queries –backup
25
DSA - XQuery25 Example 2 – see blogblog Periodic Table of Visualizations This collection of diagram types is presented very cleverly but its not very accessible. I copied the XHTML for the page and then loaded it into an eXist database – it is not well- formed XML but its good enough to upload. The links from name to example diagram are embedded in Javascript. Here is the resultresult
26
DSA - XQuery26 Exercise Some XPath examples to do in the tutorial Then continue with last week’s workshop, developing your own XML model for your chosen subject area. Next week we look at XSLT
27
DSA - XQuery27 Exercise Some XPath examples to do in the tutorial Then continue with last week’s workshop, developing your own XML model for your chosen subject area. I will talk to each of you to see what subjet you have chosen. Next week we look at XSLT
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.