XQuery Functions Reusing XQuery Code Date September 2008
XQuery Functions Reusing XQuery Code Date: September, 2008 M D Metadata Solutions Dan Mc. Creary President Dan Mc. Creary & Associates dan@danmccreary. co Copyright 2007 Dan Mc. Crearym & Associates 1
Overview • • Why use functions? XQuery Built-in functions e. Xist-specific functions How to define functions Passing parameters Function signatures Recursive functions M D Copyright 2007 Dan Mc. Creary & Associates 2
Why Use Functions? • To reuse snippets of your code • To make code easier to maintain • To make code easier to read M D Copyright 2007 Dan Mc. Creary & Associates 3
Function Layers Your Custom Functions e. Xist Extensions e. Xist built-in e. Xist XQuery Public Functions Update 1. 0 Functions Base XQuery 1. 0 Functions M D future Copyright 2007 Dan Mc. Creary & Associates 4
e. Xist Function Documentation • e. Xist has around 384 functions • tools for searching built in to each installation M D Copyright 2007 Dan Mc. Creary & Associates 5
e. Xist Function Documentation Tool M D Copyright 2007 Dan Mc. Creary & Associates 6
Common XQuery Built-in Functions • functions for getting data – doc, collection • numeric functions – abs, avg, ceiling, floor, max, min, number, round-half-to-even, sum • date and time functions – current-date. Time, current-date, current-time, implicit-timezone • set functions – distinct-values, starts-with, deep-equal, empty, subsequence • common string functions: – concat, lower-case, substring-before, substring-after, string-join, uppercase • logic – boolean, not, false, true • uri-functions – base-uri, document-uri, namespace-uri-for-prefix M D Copyright 2007 Dan Mc. Creary & Associates 7
Common e. Xist Built-in Extensions • request – processing incoming HTTP requests – create-session, get-parameter, get-data, get-session, get-context-path, get –cookie-names, get-cookie-value, get-header, get-hostname, get-method, get-server-port • response – Setting outgoing HTTP responses – redirect, set-cookie, set-header, set-status-code • xmldb – create collection, store, move, copy, change permissions, created, get-child-collections, remove, rename • update – insert, update, replace, rename, delete • session – manage session variables • system – get system status like build number, memory-free or index data • • • M D text – filter, fuzzy-match-all, fuzzy-match-any, highlight-matches, kwic-display, match-all, match-any ngram – full-text indexing contains, starts-with, ends-with, matches util – call, binary-doc, complie, eval, exclusive-lock, index-keys, log-system-out, random Copyright 2007 Dan Mc. Creary & Associates 8
Other e. Xist Extension Modules • E-mail – send an e-mail to a person from the result of an XQuery • Scheduler – schedule reports to run at intervals • Compression – compress files • Math – advanced math functions • Image Management – generate thumbnail images etc. • XML Differences – report on how XML files differ • GML – Geography Markup Language Note: These need to be configured by the e. Xist systems administrator M D Copyright 2007 Dan Mc. Creary & Associates 9
URI functions • base-uri, document-uri, static-base-uri, document-uri, iri-to-uri M D Copyright 2007 Dan Mc. Creary & Associates 10
Math and Set Functions • • floor exists insert-before insert-after M D Copyright 2007 Dan Mc. Creary & Associates 11
NGram • contains – does a full-text string contain a specific string? • starts-with, ends-with – does a full-text string start or end start with a string? • filter-matches – exclude specific results from a match http: //exist-db. org/xquery/ngram M D Copyright 2007 Dan Mc. Creary & Associates 12
How to Define a Function declare function local: my-function() as return-type { …your code here return <result/> }; M D Copyright 2007 Dan Mc. Creary & Associates 13
Hello World declare function local: hello-world() as xs: string { let $message : = 'Hello World' return $message }; M D Copyright 2007 Dan Mc. Creary & Associates 14
How to Call a Function xquery version "1. 0"; declare function local: hello-world() as xs: string { let $message : = 'Hello World' return $message }; let $my-variable : = local: hello-world() M D Copyright 2007 Dan Mc. Creary & Associates 15
Input and Output Parameters xquery version "1. 0"; declare function local: app-base-collection($uri as xs: string) as xs: string { let $app-base : = substring-before(substring-after($uri, '/exist/rest'), '/views/') return $app-base }; let $uri : = '/exist/rest/db/apps/faqs/views/my-query. xq' return <results> <app-base-collection>{local: app-base-collection($uri)}</app-base-collection> </results> <app-base-collection>/db/apps/faqs</app-base-collection> </results> M D Copyright 2007 Dan Mc. Creary & Associates 16
Get the Base Collection xquery version "1. 0"; declare function local: app-base-collection() as xs: string { let $app-base : = concat('/db/', substring-before(substring-after(request: get-uri(), '/exist/rest/db/'), '/')) return $app-base }; return <results> <uri>{request: get-uri()}</uri> <app-base-collection>{local: app-base-collection()}</app-base-collection> </results> Output: M <results> <uri> /exist/rest/db/xrx-training/labs/02 -functions/app-basecollection. xq </uri> <app-base-collection>/db/xrx-training</app-basecollection> </results> Note that this will not work from within o. Xygen, only from the web browser. D Copyright 2007 Dan Mc. Creary & Associates 17
Passing Parameters • You can pass many parameters into any function but functions usually return only a simple type or a complex element • Each parameter has a “cardinality” that describes if it is an optional parameter (may be null), or has more then one value. • The following notation is used to describe the cardinality of each parameter: none a single required parameter ? an optional single parameter * zero to many parameter + One to many parameters Note: checking parameters is a great way to test the data for your functions M D Copyright 2007 Dan Mc. Creary & Associates 18
Thank You! Please contact me for more information: • • • Metadata Management Services Web Services Service Oriented Architectures Business Intelligence and Data Warehouse Metadata Registries Semantic Web Dan Mc. Creary, President Dan Mc. Creary & Associates Metadata Strategy Development dan@danmccreary. com (952) 931 -9198 M D Copyright 2007 Dan Mc. Creary & Associates 19
- Slides: 19