Jump to content

XQuery/eXist Crib sheet

From Wikibooks, open books for an open world

Imports and Declarations

[edit | edit source]

XQuery declaration

[edit | edit source]
xquery version "1.0";

import a module

[edit | edit source]
import module namespace geo="http://www.cems.uwe.ac.uk/exist/coord" at "coord.xqm";

Base eXist namespace

[edit | edit source]
  declare namespace exist = "http://exist.sourceforge.net/NS/exist"; 

Standard eXist modules

[edit | edit source]

Optional in an XQuery script but required in a module in 1.2 but not in 1.3

 import module namespace xmldb = "http://exist-db.org/xquery/xmldb";
 import module namespace util = "http://exist-db.org/xquery/util";
 import module namespace request = "http://exist-db.org/xquery/request";
 import module namespace response = "http://exist-db.org/xquery/response";
 import module namespace session = "http://exist-db.org/xquery/session";
 import module namespace transform = "http://exist-db.org/xquery/transform";
 import module namespace text = "http://exist-db.org/xquery/text";
 import module namespace system = "http://exist-db.org/xquery/system";

declare a namespace

[edit | edit source]
 declare namespace tx="http://www.transxchange.org.uk/";

declare a default element namespace

[edit | edit source]
declare default element namespace "http://www.w3.org/1999/xhtml";

Be VERY careful if you use a default namespace. This means that everything that does not have a namespace prefix will automatically be serialized into this namespace. It also PREVENTS you from using the null namespace unless you specifically declare it as xmlns="". Note that you do NOT associate a prefix with the default namespace.

declare a default function namespace

[edit | edit source]
declare default function namespace "http://www.transxchange.org.uk/";

declare a java binding namespace

[edit | edit source]
declare namespace math="java:java.lang.Math";

Note that the java binding is disabled by default due to security issues. Math functions can now be invoked via the maths extension module.

Serialization Options

[edit | edit source]

output XML document

[edit | edit source]

This is the default, but can be declared explictly:

declare option exist:serialize "method=xml media-type=text/xml omit-xml-declaration=no indent=yes";

output SVG document

[edit | edit source]
declare option exist:serialize "method=svg media-type=application/svg+xml omit-xml-declaration=no indent=yes";


output XHTML document

[edit | edit source]
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=no indent=yes 
        doctype-public=-//W3C//DTD XHTML 1.0 Transitional//EN
        doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";

If your code contains a lot of XML fragments that are in the NULL namespace (xmlns="") then there is a method of automatically converting everything in the null namespace to automatically get put into the XHTML namespace.

declare option exist:serialize "method=xhtml enforce-xhtml=yes";

output HTML4 document

[edit | edit source]
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=no indent=yes 
        doctype-public=-//W3C//DTD HTML 4.01 Transitional//EN
        doctype-system=http://www.w3.org/TR/loose.dtd";

output HTML5 document

[edit | edit source]
declare option exist:serialize "method=html5 media-type=text/html omit-xml-declaration=yes indent=yes";

output HTML document with no doctype

[edit | edit source]
declare option exist:serialize "method=html media-type=text/html omit-xml-declaration=yes indent=yes";

output XHTML document with no doctype

[edit | edit source]
declare option exist:serialize "method=xhtml media-type=text/html omit-xml-declaration=yes indent=yes";

output plain text document with no doctype

[edit | edit source]
declare option exist:serialize "method=text media-type=text/plain omit-xml-declaration=yes";

output kml document

[edit | edit source]
declare option exist:serialize  "method=xhtml media-type=application/vnd.google-earth.kml+xml highlight-matches=none";

output XForms document

[edit | edit source]
declare option exist:serialize "method=xhtml media-type=text/xml indent=yes";

Modules and Functions

[edit | edit source]

module header

[edit | edit source]
module namespace c="http://www.cems.uwe.ac.uk/exist/coord";

function declaration

[edit | edit source]
  • in the default namespace in an XQuery script
declare function local:times($tt, $dt) {
  if (exists($dt))
  then local:times(($tt, $tt[last()]+ $dt[1]), remove($dt,1))
  else $tt
};
  • in the namespace of a module
module namespace time = 'http://www.cems.uwe.ac.uk/fold/time';
declare function time:times($tt, $dt) {
  if (exists($dt))
  then time:times(($tt, $tt[last()]+ $dt[1]), remove($dt,1))
  else $tt
};

declare variable

[edit | edit source]
declare variable $pi as xs:double := 3.14159265;

in a module with namespace prefix fxx:

declare variable $fxx:file := doc('/db/me/file.xml');

embedded CSS stylesheet

[edit | edit source]
  <style language="text/css">
  <![CDATA[
    .good {background-color: green;}
    .bad {background-color:red;}
  ]]>
  </style>

Get HTTP POST URL Parameters

[edit | edit source]

To get two URL parameters from an HTTP POST

   http://localhost:8080/exist/rest/db/my-collection/my-xquery.xq?color=blue&shape=circle

Add the following:

  let $my-color := request:get-parameter('color', 'red')
  let $my-shape := request:get-parameter('shape', '')

If no color parameter is supplied a default color of "red" will be used.

Get HTTP POST Data

[edit | edit source]

To get all the XML data from an HTTP POST

  let $my-data := request:get-data()

Extend the Output Size Limit

[edit | edit source]

By default the output size in 10,000 bytes. You can extend this by adding the following

declare option exist:output-size-limit "new-size";

For example to triple the size limit of the output use the following line:

declare option exist:output-size-limit "30000";