User-defined functions

Tuesday, 23 November 2010 · 4 messages · last reply 24 Nov 2010
CG
Christian Grünin reply to Mark Boon, 23 Nov 2010, 00:3823 Nov 2010 00:35 #
Dear Mark, thanks for your mail. Just some quick feedback: correct, user-defined functions are defined in the prolog of a query. They look as follows:
  declare function local:nameOfFunction($optionalArg) {
    ...expression...
  };
...and can be called as follows…
  local:nameOfFunction(...)
Another example is given at.. http://www.w3schools.com/XQuery/xquery_functions.asp If your function block isn't excessively long, you shouldn't care about performance, as the parsing and compilation steps are very fast in BaseX. If you should indeed encounter any bottlenecks in practice, feel free to tell us. Christian
Quoted text · Mark Boon · 29 lines
On Mon, Nov 22, 2010 at 11:41 PM, Mark Boon <mboon at avatar-reality.com> wrote: > I'm fairly new to XQuery and only started to use it and BaseX last > week. So far I've been very impressed with the capabilities of both. > > I have one question about user-defined functions. From what I can tell > so far that I learned about XQuery, user-defined functions are only > accessible when they are declared in the prolog of the query. In > itself I suppose there's a certain logic to that. My project is most > likely very atypical from your average XQuery usage, but it would be > much more convenient, and probably also much more efficient if I could > somehow (optionally) declare user-defined functions and reuse them for > several queries in a row. > > I don't think importing a module would be a solution because a) that > would be even more inefficient and b) I don't think it wouldn't work > with dynamically created functions. (I'm executing queries that > contain queries that get executed...) > > So I was wondering if there's a way to accomplish this. And even if > this is currently not possible, would it be complicated for an > accomplished Java developer to add as a custom extension? Any tips or > ideas will be greatly appreciated. > > Mark > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
MB
Mark Boon23 Nov 2010 00:38 #
I'm fairly new to XQuery and only started to use it and BaseX last week. So far I've been very impressed with the capabilities of both. I have one question about user-defined functions. From what I can tell so far that I learned about XQuery, user-defined functions are only accessible when they are declared in the prolog of the query. In itself I suppose there's a certain logic to that. My project is most likely very atypical from your average XQuery usage, but it would be much more convenient, and probably also much more efficient if I could somehow (optionally) declare user-defined functions and reuse them for several queries in a row. I don't think importing a module would be a solution because a) that would be even more inefficient and b) I don't think it wouldn't work with dynamically created functions. (I'm executing queries that contain queries that get executed...) So I was wondering if there's a way to accomplish this. And even if this is currently not possible, would it be complicated for an accomplished Java developer to add as a custom extension? Any tips or ideas will be greatly appreciated. Mark
CG
Christian Grün23 Nov 2010 22:18 #
Hi Mark, (I'm passing this on to the mailing list..)
Quoted text · Mark Boon · 11 lines
> At the moment my function block is not very long. But I expect this is > likely to change, as I'm using XQuery as a sort of programming > language where 'users' can add functions. Moreover, there will be > cases where they'll be executing thousands of queries and the total > execution time should ideally take no more than a second or two in the > most extreme circumstance. > > At this time I don't experience any performance problems, BaseX is > very snappy. But I'm still in the experimentation phase and I'd like > to avoid spending a lot of time building a framework that later may > turn out to be unworkably slow.
Regarding the complexity and size of function blocks, our users haven't reported any bottlenecks yet. Instead, bad performance is mainly due to XQuery expressions that are difficult, or impossible, to optimize. But, no doubt, it might make sense to precompile complex XQuery code that takes hundreds of KB, or even MB. Right now, precompilation of XQuery code isn't supported in BaseX
Quoted text · Mark Boon · 7 lines
> Another thing I ran into is that updating doesn't seem to be allowed > within functions. So I'm now looking into using XSLT to solve these > issues for me. On the one hand by prefixing the query with the > function declarations that are actually used in the query, and through > templates that would substitute updating function-calls by the > function-body. I'm also new to XSLT so I'm not sure if that will work, > but it seems worth giving it a try.
Updating is no problem in XQuery functions: you'll have to add the "updating" specified to a function declaration:
   declare updating function ... { ... };
A general restriction of XQuery is that an expression cannot both update and output items. As an example, the following query is invalid: delete //node, 1 to 10 Instead, you can perform local updates by applying the transform expression:
  copy $a := ...nodes to be modified...
  modify ...update on $a...
  return ...e.g., $a...
Quoted text · Mark Boon · 4 lines
> P.S. extracting the xsl-stylesheet from the file-header from an XML > file and then applying it to itself is a bit ugly, does anyone know if > there is a (JAXP) way to get an XML file with its stylesheet already > applied automagically?
Maybe this won't be necessary anymore.. Hope this helps, Christian
MB
Mark Boon24 Nov 2010 00:23 #
Quoted text · Christian Grün · 8 lines
On Tue, Nov 23, 2010 at 11:18 AM, Christian Grün <christian.gruen at gmail.com> wrote: > > Updating is no problem in XQuery functions: you'll have to add the > "updating" specified to a function declaration: > > declare updating function ... { ... }; >
Ah, I didn't know that. That solved a major headache for me, thank you.
Quoted text · Christian Grün · 7 lines
>> P.S. extracting the xsl-stylesheet from the file-header from an XML >> file and then applying it to itself is a bit ugly, does anyone know if >> there is a (JAXP) way to get an XML file with its stylesheet already >> applied automagically? > > Maybe this won't be necessary anymore.. >
Well, I already made an XSLT that selects the function-declarations and prefixes them to the queries that use them. Even with just a few functions this already greatly reduces the size of the queries. And I'll need it anyway because I plan to use XQuery as a sort of programming language, where the XML database is the (persistent) memory. And like in a normal computer, the memory contains both the data and the instructions. But since programming in XQuery is very cumbersome, I plan to have much simpler languages that will be translated into XQuery using XSLT. Mark Boon A.I. Software Engineer at Avatar Reality

⚡Generated with XQuery