-------- Original-Nachricht --------
Actually, this is perfect. Exactly what I was trying to do -- set a
global variable. I must have had the syntax wrong, because it wasn't
working. But this is great. One less thing to pass around. When you're
query involves a couple dozen functions, it gets tiresome.
Thanks much for all your help! This makes it a lot easier.
Chas.
On 01/04/2011 8:24 AM, Andreas Weiler wrote:
> Hi Chas,
>
> thanks for you example, that made it easy to understand.
> The database context isn't available in own declared functions and
> variables.
> So you could use the doc('database') function and run the following query:
>
> declare variable $data as node() := doc('database')/data;
>
> declare function local:getPersonFromID($id) {
> $data/persons/person[@id=$id]
> };
>
> local:getPersonFromID('1')
>
> So it's almost the same than your workaround.
> The doc('database') function gives you access to your database.
>
> I'm sorry there is no easier way,
> kind regards,
> Andreas
>
> Am 04.01.11 11:26, schrieb Charles F. Munat:
>> Sorry, it's hard to explain these things since I have no idea what, if
>> anything, I'm doing wrong. Here's a basic example.
>>
>> I create a database in code:
>>
>> (I'm using Scala, but hopefully this is close enough so you can see
>> what I'm doing.)
>>
>> private val context = new Context()
>> new org.basex.core.cmd.Set("dbpath", "/var/www/xxx/db").execute(context)
>>
>> try {
>> new Open("data").execute(context)
>> } catch {
>> case _ => new CreateDB("data", "<data/>").execute(context)
>> }
>>
>> So now I have a new database that contains only <data/>. I now load it:
>>
>> val file = "/var/www/xxx/content/persons.xml"
>> new XQuery("insert node " +
>> XML.loadFile(file).toString +
>> " into /data").execute(context)
>>
>> Now my data is in the embedded database. Suppose the file looked like
>> this:
>>
>> <persons>
>> <person id="1">
>> <names>
>> <given>John</given>
>> <family>Lennon</family>
>> </names>
>> </person>
>> <person>
>> <names id="2">
>> <given>Paul</given>
>> <family>McCartney</family>
>> </names>
>> </person>
>> </persons>
>>
>> Now I can run an XQuery like this (really just XPath):
>>
>> /data/persons/person[@id='1']
>>
>> And I'll get back:
>>
>> <person id="1">
>> <names>
>> <given>John</given>
>> <family>Lennon</family>
>> </names>
>> </person>
>>
>> Simple, right?
>>
>> But if I do this:
>>
>> declare function local:getPersonFromID($id) {
>> /data/persons/person[@id=$id]
>> };
>>
>> local:getPersonFromID('1')
>>
>> Then I get this error:
>>
>> No context item set for 'root()'.
>>
>> The workaround I've been using is this:
>>
>> declare function local:getPersonFromID($data, $id) {
>> $data/persons/person[@id=$id]
>> };
>>
>> let $data := /data
>> return local:getPersonFromID('1')
>>
>> There must be a better way.
>>
>> Does this make more sense?
>>
>> Chas.
>>
>> On 01/04/2011 5:47 AM, Andreas Weiler wrote:
>>> 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:
>>>>
>>>> 1. 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.
>>>>
>>>> 2. 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
>>>
>