Bind optional variables and/or bind node
Hello,
I want to query a BaseX XML Database via Python (BaseXClient.py). Therefore I write a XQuery script, which executes a FLOWR expression combined with a few self-written functions. A bunch of options, which are generated within my Python script, have to externally bind to this query.
For convenience it would be appropriate to have optional external variables which I do not have necessarily to bind, but which will have default values specified in the XQuery script. Is there a possibility to do this?
I thought about an alternative, that is to declare the options with the help of a XML snippet. But then I could not figure out how to bind this snippet as a node instead of a string, because XQuery itself does not have a function to convert a String containing a XML document to a node. Of course I could create before each request a new options document in the database, but this seems to be a bit unhandy.
All in all I have the impression, that those are quite easy problems, but for some reason I could not find any solution on the web, so I were glad about reading a solution to one of those problems.
All the best, Ronny
Hi Ronny I use BaseX to do some transformatins over Pyton binding.
In case I test it in GUI, I have in my current collection one source document. When I process it remtoely, I pass the data as string and convert it into xml document.
So in my XQuery code I have this:
(:
let $xml := collection()[1]
:)
let $xml := util:eval($xmlString)The later is used in production and is converting incoming string into xml doc.
Jan
*Jan Vlčinský* TamTam Research s.r.o. Slunečnicová 338/3, 734 01 Karviná Ráj, Czech Republic tel: +420-597 602 024; mob: +420-608 979 040 skype: janvlcinsky; GoogleTalk: jan.vlcinsky at gmail.com http://cz.linkedin.com/in/vlcinsky
Quoted text · Ronny Möbius · 32 lines
On 13 March 2012 17:08, Ronny Möbius <moebius at physik.hu-berlin.de> wrote: > Hello, > > I want to query a BaseX XML Database via Python (BaseXClient.py). > Therefore I write a XQuery script, which executes a FLOWR expression > combined with a few self-written functions. A bunch of options, which > are generated within my Python script, have to externally bind to this > query. > > For convenience it would be appropriate to have optional external > variables which I do not have necessarily to bind, but which will have > default values specified in the XQuery script. Is there a possibility to > do this? > > I thought about an alternative, that is to declare the options with the > help of a XML snippet. But then I could not figure out how to bind this > snippet as a node instead of a string, because XQuery itself does not > have a function to convert a String containing a XML document to a node. > Of course I could create before each request a new options document in > the database, but this seems to be a bit unhandy. > > All in all I have the impression, that those are quite easy problems, > but for some reason I could not find any solution on the web, so I were > glad about reading a solution to one of those problems. > > All the best, > Ronny > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
Hi Jan, Hi Ronny,
maybe you might want to use the following approach:
Quoted text · Jan Vlčinský (TamTam Research) · 4 lines
> declare variable $a external; > let $default := "<b>I am default</b>" > let $vals := ( try { $a } catch * {()}, $default )[1] > return parse-xml($vals)
the try clause tries to assign $a to $vals, and if catched assigns the empty sequence to $vals.
The [1] predicate selects the first element of this sequence, as empty subsequences are flattened, the value for $default is returned if $a has been set to the empty sequence.I chose parse-xml instead instead of util:eval; might be both a little faster and definitely more secure :-)
Hope this helps (and works ;-)).
Kind Regards
Michael
Quoted text · Jan Vlčinský (TamTam Research) · 2 lines
Am 13.03.2012 um 17:20 schrieb Jan Vlčinský (TamTam Research): > let $xml := util:eval($xmlString)
Hi Jan, Hi Michael,
thank you both very much. I was not aware of the parse-xml function, because I only read a reference of XQuery2 functions.
The try .... catch statement is also very helpful.
Thanks a lot! Ronny
Quoted text · Michael Seiferle · 29 lines
On 03/13/2012 06:35 PM, Michael Seiferle wrote: > Hi Jan, > Hi Ronny, > > maybe you might want to use the following approach: >> declare variable $a external; >> let $default := "<b>I am default</b>" >> let $vals := ( try { $a } catch * {()}, $default )[1] >> return parse-xml($vals) > > the try clause tries to assign $a to $vals, and if catched assigns the > empty sequence to $vals. > The [1] predicate selects the first element of this sequence, as empty > subsequences are flattened, the value for $default is returned if $a has > been set to the empty sequence. > > I chose parse-xml instead instead of util:eval; might be both a little > faster and definitely more secure :-) > > Hope this helps (and works ;-)). > > Kind Regards > > Michael > > Am 13.03.2012 um 17:20 schrieb Jan Vlčinský (TamTam Research): > >> let $xml := util:eval($xmlString) >
Written for BaseX 7.1. BaseX may have changed since: please check the current documentation.