160 likes | 276 Views
Introduction to XQuery and eXist. XQuery workshop April 2006. Whisky Case study. Starting point is the Collins Gem Whisky book. Based on extracts, students construct a schema. Schema diagrammed using QSEE. XML Schema generated by QSEE.
E N D
Introduction to XQuery and eXist XQuery workshop April 2006
Whisky Case study • Starting point is the Collins Gem Whisky book. • Based on extracts, students construct a schema. • Schema diagrammed using QSEE. • XML Schema generated by QSEE. • InfoPath used to build an input form based on the XML schema. • Sample data entered using the InfoPath form interface. • Data loaded into eXist for querying.
<?mso-infoPathSolution productVersion="11.0.6565" PIVersion="1.0.0.0" href="file:whisky.xsn" name="urn:schemas-microsoft-com:office:infopath:whisky:" language="en-gb" solutionVersion="1.0.0.5" ?><?mso-application progid="InfoPath.Document"?> <Whisky> <Distillery> <did>132</did> <Name>Glen Garioch</Name> <Address>Glengarioch Distillery, Oldemldrum, Aberdeenshire</Address> <WhiskyType>Blend</WhiskyType> <BottlingAge>8</BottlingAge> <BottlingAge>10</BottlingAge> <BottlingAge>12</BottlingAge> <BottlingAge>15</BottlingAge> <BottlingAge>21</BottlingAge> <Strength>43</Strength> <TasteRating>3</TasteRating> <Miniatures>true</Miniatures> <WhiskyDescription>This medium-bodied whisky, with its light texture and smoky flavour, is an ideal after-dinner dram.</WhiskyDescription> <History>Set in the Aberdeenshire market town of Oldmeldrum, Glengarioch was reputedly founded in the 1790's.</History> <Visitors>By_Appointment</Visitors> <TelephoneNo>06512 2706</TelephoneNo> <OpeningTimes/> <OperatingStatus>Running</OperatingStatus> <Availability/> <WhiskyTrail>false</WhiskyTrail> <ownerdid/> </Distillery> </Whisky>
Executing an XQuery eXist: Server Client Browser User clicks link Get a.xql parameters servlet fetch a.xql parameters a.xql eXist DB html XQuery Engine render
Simple XQuery xquery version "1.0"; <table border='2'> <tr><th>Name</th><th>Address</th></tr> {for $w in doc('/db/whisky/data_raw.xml')/Whisky/Distillery return <tr><td>{data($w/Name)}</td> <td>{data($w/Address)}</td> </tr> } </table> addressList.xql
XQuery as HTML xQuery header xquery version "1.0"; <table border='2'> <tr><th>Name</th><th>Address</th></tr> {for $w in doc('/db/whisky/data_raw.xml')/Whisky/Distillery return <tr><td>{data($w/Name)}</td> <td>{data($w/Address)}</td> </tr> } </table> Xquery inside XML XPath expression to select nodes XQuery ‘variable’ addressList.xql
Executing an XQuery with XSLT eXist: Server Client Browser User clicks link Get a.xql parameters servlet fetch a.xql parameters a.xql eXist DB XQuery Engine .xml xslt html XSLT Engine render
Simplest XQuery -XSLT xquery version "1.0"; declare namespace transform = "http://exist-db.org/xquery/transform"; let $stylesheet := doc('/db/whisky/whisky.xslt'), $whiskySet := doc('/db/whisky/data_raw.xml') return transform:transform($whiskySet,$stylesheet,()) allWhiskys.xql
Simplest XQuery -XSLT Comma separator xquery version "1.0"; declare namespace transform = "http://exist-db.org/xquery/transform"; let $stylesheet := doc('/db/whisky/whisky.xslt'), $whiskySet := doc('/db/whisky/data_raw.xml') return transform:transform($whiskySet,$stylesheet,()) Retrieve this document a FLWOR expression namespace prefix allWhiskys.xql
XQuery with parameter xquery version=“1.0”; declare namespace transform = "http://exist-db.org/xquery/transform"; declare namespace request="http://exist-db.org/xquery/request"; let $whiskyType := request:request-parameter ("whiskyType",""), $stylesheet := doc('/db/whisky/whisky.xslt'), $whiskySet := <Whisky> {doc('/db/whisky/data_raw.xml')/Whisky/Distillery [WhiskyType = $whiskyType]} </Whisky> return transform:transform($whiskySet,$stylesheet,()) selectWhiskys.xql selectWhiskys.xql
XQuery with parameter xquery version=“1.0”; declare namespace transform = "http://exist-db.org/xquery/transform"; declare namespace request="http://exist-db.org/xquery/request"; let $whiskyType := request:request-parameter ("whiskyType",""), $stylesheet := doc('/db/whisky/whisky.xslt'), $whiskySet := <Whisky> {doc('/db/whisky/data_raw.xml')/Whisky/Distillery [WhiskyType = $whiskyType]} </Whisky> return transform:transform($whiskySet,$stylesheet,()) Get the value of the request parameter with this name Create an XML node XPath filter selectWhiskys.xql
XQuery is bi-lingualXML and XQuery • XML node (with a single root) <a> <b> h</b> </a> • XML Sequence of nodes (<a/>, <b/>, <c/>) • XQuery construct variable value $var function evaluation – see Function list concat($var,’.xm’) for loop FLWOR if-then-else if (cond) then expression else expression • Mixed expressions – need { } around XQuery to distinguish • <a> {if .. } {$x}</a> • if (cond) then <a> Fred </a> else ()
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)
An XQuery sticky form • The script here has several shifts between XML to XQuery • Whole interface in one script • Equivalent to PHP + MySQL • XSLT revised to process a sequence of Distillery nodes into a sequence of HTML nodes.
Tools for XQuery on stocks • Use a text editor (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 test query interface to test small bits of XQuery and XPath
eXist on your own machine • Use the Java client to • load files • move, copy,rename files • edit files in situ (but no Save-as) • execute queries • backup