Thanks! I prefer the first option. It gives me access without having to pass something on every function call. Good point about the side effects; I'll have to keep that in mind.
Chas.
On 01/04/2011 8:48 AM, Michael Seiferle wrote:
Hey Chas, Hey Andi,
Andis Mail pointed me into the right direction and I guess you could simply do the following, instead of passing around the db nodes
declare variable $_context := doc('data'); declare function local:getPersonFromID($id) { $_context//persons/person[@id=$id] }; local:getPersonFromID('1')
Or you could as well pass the db name and open it dynamically in your function
declare function local:getPersonFromID($id, $dbn) { doc($dbn)//persons/person[@id=$id] }; local:getPersonFromID('1','data')
The current context is never implicitly passed on to functions. This is to avoid side effects; so a function is only defined by its signature. Without doubt the solution I propose here should not be taken as a best practice but probably suits your case well enough.
There probably are some better solutions around. Maybe s/o else on the list knows of one.
Hope this helps! In case it does not, feel free to ask for more.
Kind regards
Michael
Am 04.01.2011 um 09:47 schrieb Andreas Weiler:
Hi Chas,
i actually don't understand what your concrete problem is, but you could have a look in our xquery database functions: http://www.inf.uni-konstanz.de/dbis/basex/xq-db
If you have opened the database before running queries, the whole database is in your context and so it is used in the queries.
Kind regards, Andreas
Am 04.01.11 00:54, schrieb Charles F. Munat:
I have an embedded Base-X database in a web application. I have a huge XQuery that involves a lot of local functions, many of them recursive. In several of these functions, I need to query the database to look up referenced elements. I can't for the life of me figure out a way to access the database without doing one of two things:
My document root is /data. I can let $root := /data and then pass that into every function. Passing things into functions rather than setting global variable is certainly and FP way of doing things, but in this instance it seems to me that it should be the scope I'm working in.
If something else I pass into the function is a node in the database, then I can, for example, use $node/root() to back up to the root node. But sometimes I'm only passing in a string and I still need to query the whole database.
Is there some way to access the root easily from within a function? I am very confused by the whole "context" idea. I've looked all over, but I must be searching using the wrong terms, or it simply isn't done.
Anyone know a way?
Thanks,
Chas. _______________________________________________ BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk