Server-side XQuery scripts understanding their "context"

Tuesday, 21 January 2020 · 4 messages · last reply 22 Jan 2020
EL
ERRINGTON Luke21 Jan 2020 02:52 #
Hi all, I'm trying to use the REST API to call server-side XQuery scripts. As I understand it, these scripts are not part of a database, but reside in the filesystem where BaseX is "installed" (or where RESTPATH points to). However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query? For example, http://localhost:8984/rest?run=find.xq http://localhost:8984/rest/Test?run=find.xq ... both execute the same query, and that query needs to open a database to do its searching, so currently the database to open is hard-coded. It would be nice if the query could work out what database or "collection" was its context and automatically use that. Additionally, if I do http://localhost:8984/rest?run=Test/find.xq ... then I run the query that is in the Test folder, but this still does not have any context and so still needs to know to open the Test database. Is there a way to achieve this without having to pass the name of the database to every XQuery? Or am I not understanding things and is there a better way to do this sort of thing? Thanks, Luke
CG
Christian Grün21 Jan 2020 09:07 #
Hi Luke,
Quoted text · ERRINGTON Luke · 1 line
> However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query?
If you address a database in your URL, it will be bound to the context of your query, which can be obtained via a simple dot (.). For example, run the following query to get the number of documents in your 'Test' database: http://localhost:8984/rest/Test?query=count(.) I have added another example in our documentation [1]. The given query can also be written to a file and evaluated with 'run'. Hope this helps, Christian [1] http://docs.basex.org/wiki/REST#GET_Method
EL
ERRINGTON Luke22 Jan 2020 02:33 #
Hi Christian, Thanks for that. That's exactly what I was hoping for (and I thought I tried that, but must have done something wrong). Now for the trickier part of the problem: Let's say that I want to add a file to the database, but I want to do it through a server-side XQuery, so that it can validate, etc. Can I do this using the context of the call, or do I have to pass the name of the database as an argument? e.g.
db:add("Test", <a/>, "doc.xml") (: Works, but has hardcoded the database :)
==8<=========================
let $db := request:parameter('param1')
return
db:add($db, <a/>, "doc.xml") (: Works, but had to pass the database name as a parameter :)
==8<=========================
db:add(., <a/>, "doc.xml") (: Doesn't work, and I didn't expect it to, but would be ideal :)
Is there any way to achieve this? There isn't anyway of converting the current context into an expression containing the database (and path) is there? Thanks, Luke
Quoted text · Christian Grün · 14 lines
-----Original Message----- From: Christian Grün <christian.gruen at gmail.com> Sent: Tuesday, 21 January 2020 6:38 PM To: ERRINGTON Luke <Luke.Errington at sydac.com> Cc: basex-talk at mailman.uni-konstanz.de Subject: Re: [basex-talk] Server-side XQuery scripts understanding their "context" Hi Luke, > However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query? If you address a database in your URL, it will be bound to the context of your query, which can be obtained via a simple dot (.). For example, run the following query to get the number of documents in your 'Test' database: http://localhost:8984/rest/Test?query=count(.) I have added another example in our documentation [1]. The given query can also be written to a file and evaluated with 'run'. Hope this helps, Christian [1] http://docs.basex.org/wiki/REST#GET_Method
CG
Christian Grün22 Jan 2020 12:59 #
Hi Luke, That’s a classical recommendation for RESTXQ, which gives you a more powerful and flexible framework for writing custom REST APIs. Did you already have a look at it? Best, Christian
Quoted text · ERRINGTON Luke · 50 lines
On Wed, Jan 22, 2020 at 2:33 AM ERRINGTON Luke <Luke.Errington at sydac.com> wrote: > > Hi Christian, > > Thanks for that. That's exactly what I was hoping for (and I thought I tried that, but must have done something wrong). > > Now for the trickier part of the problem: > > Let's say that I want to add a file to the database, but I want to do it through a server-side XQuery, so that it can validate, etc. Can I do this using the context of the call, or do I have to pass the name of the database as an argument? > > e.g. > > db:add("Test", <a/>, "doc.xml") (: Works, but has hardcoded the database :) > > ==8<========================= > > let $db := request:parameter('param1') > return > db:add($db, <a/>, "doc.xml") (: Works, but had to pass the database name as a parameter :) > > ==8<========================= > > db:add(., <a/>, "doc.xml") (: Doesn't work, and I didn't expect it to, but would be ideal :) > > Is there any way to achieve this? There isn't anyway of converting the current context into an expression containing the database (and path) is there? > > Thanks, > Luke > > -----Original Message----- > From: Christian Grün <christian.gruen at gmail.com> > Sent: Tuesday, 21 January 2020 6:38 PM > To: ERRINGTON Luke <Luke.Errington at sydac.com> > Cc: basex-talk at mailman.uni-konstanz.de > Subject: Re: [basex-talk] Server-side XQuery scripts understanding their "context" > > Hi Luke, > > > However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query? > > If you address a database in your URL, it will be bound to the context of your query, which can be obtained via a simple dot (.). For example, run the following query to get the number of documents in your 'Test' database: > > http://localhost:8984/rest/Test?query=count(.) > > I have added another example in our documentation [1]. The given query can also be written to a file and evaluated with 'run'. > > Hope this helps, > Christian > > [1] http://docs.basex.org/wiki/REST#GET_Method

⚡Generated with XQuery