4

Let's take the most simple function I can think of written in xquery:

declare function local:identityFunction($v as xs:integer) 
{
  return ($v)
}; 

Where do I declare it?

I am trying both exist-db and basex, but if I write it in the query processor window, they give me some errors (though normal xqueries work).

For example basex complains with the following message: "Expecting expression".

Joe Wicentowski
  • 5,159
  • 16
  • 26
Kami
  • 1,079
  • 2
  • 13
  • 28

1 Answers1

5

You can insert them before the normal expression.

Your mistake is to use 'return', which is neither needed nor allowed there, since a xquery function always returns the last (and only) value.

And the semicolon should be followed with another expression.

Therefore this will work:

declare function local:identityFunction($v as xs:integer) 
{
  $v
};     
local:identityFunction(17)
BeniBela
  • 16,412
  • 4
  • 45
  • 52
  • Thank you very much. I knew it was a simple problem. Can I ask you how xml databases manage these functions? In relationale databases you can store them. Is it the same also in this case? – Kami Jan 24 '13 at 13:53
  • I do not actually know anything about databases.. But in standalone xquery you can either keep them within the expression or put them in modules for them. – BeniBela Jan 24 '13 at 14:00
  • Ok thank you. For module, you mean a simple text file with all these declarations? – Kami Jan 24 '13 at 14:06
  • It is a little bit more complex. You start with, e.g. `module namespace test = "pseudo://test-module";` and call all functions `test:functionname`, and then you can use `import module "pseudo://test-module"; ` to load all these functions. (how it actually finds the file with the functions depends on the implementation) – BeniBela Jan 24 '13 at 14:11
  • In eXist-db you can store your modules as .xq files, and then call them through your browser. For exmample, you could store my-module.xq in /db/apps/my-app/my-module.xq, and then you could execute this query by accessing http://localhost:8080/exist/apps/my-app/my-module.xq. There are several ways to upload files into eXist-db. See http://demo.exist-db.org/exist/apps/doc/uploading-files.xml. – Joe Wicentowski Jan 25 '13 at 19:25