A free-form XQuery through RESTXQ?
Saturday, 11 January 2014 · 3 messages
NJ
Neven Jovanović11 Jan 2014 17:18 #
Hello,
my name is Neven Jovanovic, and I have beginner's problems with BaseX RESTXQ interface. We'd like to have a textarea form where users would compose their own XQueries to be executed over our database (just querying it, no updating etc). I have already written a cgi Perl script which does this through BaseX REST interface. Now I'd like to do the same with RESTXQ, but I'm missing something.
For testing purposes, we use the .zip version of 7.7.2 release, on Linux Mint Debian (locally), started with .basexhttp. Everything works OK, the db is there etc.
Here is the form added to the existing restxq.xqm:
<h3>Write an XQuery</h3>
<p>Compose your own XQuery.</p>
<form method="post" action="compoxq">
<p>XQuery:<br />
<textarea name="query" rows="5" cols="80"></textarea><br/>
<input type="submit" /></p>
</form>Here is the function added to the restxq.xqm:
(:~
: A free-form XQuery.
:)
declare
%rest:path("/compoxq")
%rest:POST
%rest:form-param("query","{$query}", "(no query)")
function page:cpxq-postman(
$query as xs:string*)
{
let $q := $query
return element q {
collection("crobib")//$q
}
};Submitting the following form string:
//*:person
I get these results at <http://localhost:8984/compoxq>:
<q>
//*:person //*:person //*:person //*:person //*:person (lots more...)
</q>Some query obviously is being performed, but instead of results, I get the query string.
Appreciating your help,
Neven
Neven Jovanovic
University of Zagreb
Hrvatska / Croatia
AG
Arve Gengelbach11 Jan 2014 19:02 #
Hi Neven,
have a look at the [XQuery Module] especially at xquery:eval() or xquery:evaluate() to evaluate a given string. For security reasons you should perhaps bind your database as context, e.g. xquery:eval($query, map{ '' := db:open('crobib') })
If you’d just want to allow XPathes, you could make use of the fn:path() function, e.g. db:open('crobib')//*[path(). = $query]Hope this helps
Arve
[XQuery Module] http://docs.basex.org/wiki/XQuery_Module
Quoted text · Neven Jovanović · 64 lines
On 11 Jan 2014, at 17:18, Neven Jovanović <filologanoga at gmail.com> wrote: > Hello, > > my name is Neven Jovanovic, and I have beginner's problems with BaseX > RESTXQ interface. We'd like to have a textarea form where users would > compose their own XQueries to be executed over our database (just > querying it, no updating etc). I have already written a cgi Perl > script which does this through BaseX REST interface. Now I'd like to > do the same with RESTXQ, but I'm missing something. > > For testing purposes, we use the .zip version of 7.7.2 release, on > Linux Mint Debian (locally), started with .basexhttp. Everything works > OK, the db is there etc. > > Here is the form added to the existing restxq.xqm: > > <h3>Write an XQuery</h3> > <p>Compose your own XQuery.</p> > <form method="post" action="compoxq"> > <p>XQuery:<br /> > <textarea name="query" rows="5" cols="80"></textarea><br/> > <input type="submit" /></p> > </form> > > Here is the function added to the restxq.xqm: > > (:~ > : A free-form XQuery. > :) > declare > %rest:path("/compoxq") > %rest:POST > %rest:form-param("query","{$query}", "(no query)") > function page:cpxq-postman( > $query as xs:string*) > { > let $q := $query > return element q { > collection("crobib")//$q > } > }; > > Submitting the following form string: > //*:person > > I get these results at <http://localhost:8984/compoxq>: > <q> > //*:person //*:person //*:person //*:person //*:person (lots more...) > </q> > > Some query obviously is being performed, but instead of results, I get > the query string. > > Appreciating your help, > > Neven > > Neven Jovanovic > University of Zagreb > Hrvatska / Croatia > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
NJ
Neven Jovanović11 Jan 2014 22:49 #
Arve, great! It was just what I needed.
My function now looks like this:
function page:cpxq-postman(
$query as xs:string*)
{
let $q := xquery:eval($query, map{ '' := db:open('crobib') })
return element v { $q }
};And it works as expected. I also understand now how to call functions provided by various BaseX modules. The XPath solution will come in useful as well.
Thanks to you and all the best to the whole team,
Neven
Neven Jovanovic
University of Zagreb
Hrvatska / Croatia
Quoted text · Arve Gengelbach · 78 lines
On 11 January 2014 19:02, Arve Gengelbach <ag at basex.org> wrote: > Hi Neven, > > have a look at the [XQuery Module] especially at xquery:eval() or xquery:evaluate() to evaluate a given string. For security reasons you should perhaps bind your database as context, e.g. xquery:eval($query, map{ '' := db:open('crobib') }) > If you’d just want to allow XPathes, you could make use of the fn:path() function, e.g. db:open('crobib')//*[path(). = $query] > > Hope this helps > Arve > > [XQuery Module] http://docs.basex.org/wiki/XQuery_Module > > > On 11 Jan 2014, at 17:18, Neven Jovanović <filologanoga at gmail.com> wrote: > >> Hello, >> >> my name is Neven Jovanovic, and I have beginner's problems with BaseX >> RESTXQ interface. We'd like to have a textarea form where users would >> compose their own XQueries to be executed over our database (just >> querying it, no updating etc). I have already written a cgi Perl >> script which does this through BaseX REST interface. Now I'd like to >> do the same with RESTXQ, but I'm missing something. >> >> For testing purposes, we use the .zip version of 7.7.2 release, on >> Linux Mint Debian (locally), started with .basexhttp. Everything works >> OK, the db is there etc. >> >> Here is the form added to the existing restxq.xqm: >> >> <h3>Write an XQuery</h3> >> <p>Compose your own XQuery.</p> >> <form method="post" action="compoxq"> >> <p>XQuery:<br /> >> <textarea name="query" rows="5" cols="80"></textarea><br/> >> <input type="submit" /></p> >> </form> >> >> Here is the function added to the restxq.xqm: >> >> (:~ >> : A free-form XQuery. >> :) >> declare >> %rest:path("/compoxq") >> %rest:POST >> %rest:form-param("query","{$query}", "(no query)") >> function page:cpxq-postman( >> $query as xs:string*) >> { >> let $q := $query >> return element q { >> collection("crobib")//$q >> } >> }; >> >> Submitting the following form string: >> //*:person >> >> I get these results at <http://localhost:8984/compoxq>: >> <q> >> //*:person //*:person //*:person //*:person //*:person (lots more...) >> </q> >> >> Some query obviously is being performed, but instead of results, I get >> the query string. >> >> Appreciating your help, >> >> Neven >> >> Neven Jovanovic >> University of Zagreb >> Hrvatska / Croatia >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
Written for BaseX 7.7. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery