Using "define function ...."

Tuesday, 13 July 2010 · 3 messages
J
johanna.vompras13 Jul 2010 15:02 #
Hi, I am relatively new to baseX and XQuery and just trying the funcionalities of BaseX. I have got some questions on the possibility of defining user-defined functions, which take a node (or a node set) as parameters and process them subsequently (e.g. format the results as an html table, or another numerical computations etc.). This might look like this:
define function createTable($n as node)
{
 for $c in $n/* return <some html-output>;
}
and I would like to use the defined function "createTable" in a xquery expression. For example :
for $questionscheme in doc("database")//d:QuestionScheme return createTable($questionscheme/node())
That means I need to pass a whole node (not the query results as string), in order to process the node with further xquery expressions. Is this compatible with BaseX? And where should I define such functions? Defining a function through the XQuery-API does not work. The functions in class Query in the BaseXClient.php seem to only support a successive query processing (one result tuple after another). Or have I overseen something? If not, can this class be quickly modified to support this? Many thanks for your help on this!
CG
Christian Grün13 Jul 2010 16:24 #
Johanna, specifying nodes as arguments is straight-forward in XQuery; this expression might do what you want:
declare function local:createTable($n as node()) as node()* {
  for $c in $n/*
  return <result/>
};
for $node in doc("database")//node
return local:createTable($node)
Hope this helps, Christian
Quoted text · johanna.vompras · 24 lines
On Tue, Jul 13, 2010 at 3:02 PM, <johanna.vompras at uni-bielefeld.de> wrote: > Hi, I am relatively new to baseX and XQuery and just trying the funcionalities of BaseX. I have got some questions on the possibility of defining user-defined functions, which take a node (or a node set) as parameters and process them subsequently (e.g. format the results as an html table, or another numerical computations etc.). > > This might look like this: > > define function createTable($n as node) > { > for $c in $n/* return <some html-output>; > } > > and I would like to use the defined function "createTable" in a xquery expression. For example : > > for $questionscheme in doc("database")//d:QuestionScheme return createTable($questionscheme/node()) > > That means I need to pass a whole node (not the query results as string), in order to process the node with further xquery expressions. Is this compatible with BaseX? And where should I define such functions? Defining a function through the XQuery-API does not work. The functions in class Query in the BaseXClient.php seem to only support a successive query processing (one result tuple after another). Or have I overseen something? If not, can this class be quickly modified to support this? > > Many thanks for your help on this! > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
CG
Christian Grün13 Jul 2010 17:45 #
Hi Johanna, it looks as if your quoted example from the »XQuery Kick Start« books is XQuery-only, ignoring other languages such as PHP, so (I guess?) it's quite comparable to the example I proposed below. The only difference is the XQuery syntax, which has changed in the final W3 Recommendation: Old: define function func() { ... }; Final: declare function local:func() { ... }; Regarding the mix of PHP and XQuery, unfortunately, you won't be able to "import" any XQuery functions. Instead, you'll have to decide which world world you prefer – currently, they are just too different to go hand in hand. Does this come closer to your initial question? Christian
Quoted text · Johanna Vompras · 205 lines
On Tue, Jul 13, 2010 at 5:24 PM, Johanna Vompras <johanna.vompras at uni-bielefeld.de> wrote: > Hi Christian, > > thanks for the quick answer and the example! Yes, Xquery seems to have > many advantages. Although this example do not 100% match what i have in > mind. My intention is first to query the XML Database and transfer the > results to a function (in the above example FUNCTION) > This function should then dynamically be invoked on the results. > > $cmd = 'for $node in doc("factbook")//country return FUNCTION($node)'; > > But where can I define this function withhin my .php file ... > Is there a possibility to say "import FUNCTIONSFILE.xquery" ???? > Maybe I understand something wrong ... > > I have also found this example, it is nearly that what I have in mind > (but I didn't get it to run!) > > http://www.informit.com/articles/article.aspx?p=101764&seqNum=11 > > In principle, I want to apply external functions on my xquery results. > > Hope it does not sount very confusing ... > > Thanks for helping > > Johanna. > > > > Am 13.07.2010 16:59, schrieb Christian Grün: >> Hi Johanna, >> >> your questions are welcome – I'm just forwarding them back to the >> mailing-list, as they might be interesting for others as well. >> >> The most elegant way to use XML is actually to build the larger >> snippets in XQuery (although our php table example points to another >> direction…). In the beginning, this might sound tedious, but it's >> worth the time; XQuery is pretty simple, compared to e.g. XSLT, and, >> yet, very manifold and powerful enough to construct very complex >> XML/HTML snippets. The BaseX GUI Editor might help you to get to know >> the language (you'll immediately see the query results after each key >> click if you choose "Options->Realtime Execution"). >> >> XQuery is often called an »Information Processing Language«, which >> means that it allows much more than other query languages, such as >> SQL. >> >> The following snippet shows you how to construct an HTML table via XQuery: >> >> (: Beginning of Query :) >> <table border='0'> >> <tr> >> <th>Name</th> >> <th>Population</th> >> </tr> >> { >> for $country at $p in //country >> let $color := if($p mod 2) then '#eeeeee' else '#ffffff' >> return >> <tr style="background-color:{ $color };"> >> <td>{ data($country/name) }</td> >> <td>{ data($country/@population) }</td> >> </tr> >> } >> </table> >> (: End of Query :) >> >> Note that your XQuery results will always be well-formed, which isn't >> the case for PHP. Said this, it is even more elegant to construct >> complete HTML documents in XQuery – but this will surely render the >> source files more unreadable for non-XQuery-folks. >> >> Feel free to ask for more, >> Christian >> >> ___________________________ >> >> On Tue, Jul 13, 2010 at 4:34 PM, Johanna Vompras >> <johanna.vompras at uni-bielefeld.de> wrote: >>> Hi Christian, >>> >>> Thank you! >>> I have just tried it out (throught the BaseX-Api) and it works. My Question >>> now is, how to link this a functionality into a .php file, e.g. the >>> TableExample.php which can be downloaded from >>> https://svn.uni-konstanz.de/dbis/basex/trunk/api/etc/php/ . >>> It looks like: >>> >>> include("BaseXClient.php"); >>> >>> // commands to be performed >>> $cmd = 'for $node in doc("factbook")//country order by >>> xs:int($node/@population) return data($node/@name)'; >>> $cmd2 = 'for $node in doc("factbook")//country order by >>> xs:int($node/@population) return data($node/@population)'; >>> >>> try { >>> // create session >>> $session = new Session("localhost", 1984, "admin", "admin"); >>> >>> echo "<table border='0' cellspacing='2' cellpadding='4' width='20%'> >>> <tbody><tr style='text-align:center;'><tbody><tr >>> style='text-align:center;'>"; >>> echo "<td style='text-align:center;background-color:#D7D7D7;border:#ffffff >>> 1px solid;font-size:12pt;'></td>"; >>> echo "<td style='text-align:center;background-color:#D7D7D7;border:#ffffff >>> 1px solid;font-size:12pt;'>Country</td>"; >>> echo "<td style='text-align:center;background-color:#D7D7D7;border:#ffffff >>> 1px solid;font-size:12pt;'>Population</td>"; >>> try { >>> $query = $session->query($cmd); >>> $query2 = $session->query($cmd2); >>> $count = 0; >>> while($query->more()) { >>> >>> ............ etc ... >>> >>> Where schould I define a such user-defined function, so I can allready use >>> it in the xquery statement? In the $cmd variable before the xquery >>> expression? It is not an elegant way .... Is there a possibility to include >>> some XQuery functions from en external file, by saying: "include ...." ??? >>> >>> Sorry for so much questions!! >>> >>> Thanks! >>> >>> Johanna. >>> >>> >>> >>> Am 13.07.2010 16:24, schrieb Christian Grün: >>>> >>>> Johanna, >>>> >>>> specifying nodes as arguments is straight-forward in XQuery; this >>>> expression might do what you want: >>>> >>>> declare function local:createTable($n as node()) as node()* { >>>> for $c in $n/* >>>> return<result/> >>>> }; >>>> for $node in doc("database")//node >>>> return local:createTable($node) >>>> >>>> >>>> Hope this helps, >>>> Christian >>>> >>>> >>>> >>>> On Tue, Jul 13, 2010 at 3:02 PM,<johanna.vompras at uni-bielefeld.de> wrote: >>>> >>>>> >>>>> Hi, I am relatively new to baseX and XQuery and just trying the >>>>> funcionalities of BaseX. I have got some questions on the possibility of >>>>> defining user-defined functions, which take a node (or a node set) as >>>>> parameters and process them subsequently (e.g. format the results as an html >>>>> table, or another numerical computations etc.). >>>>> >>>>> This might look like this: >>>>> >>>>> define function createTable($n as node) >>>>> { >>>>> for $c in $n/* return<some html-output>; >>>>> } >>>>> >>>>> and I would like to use the defined function "createTable" in a xquery >>>>> expression. For example : >>>>> >>>>> for $questionscheme in doc("database")//d:QuestionScheme return >>>>> createTable($questionscheme/node()) >>>>> >>>>> That means I need to pass a whole node (not the query results as string), >>>>> in order to process the node with further xquery expressions. Is this >>>>> compatible with BaseX? And where should I define such functions? Defining a >>>>> function through the XQuery-API does not work. The functions in class Query >>>>> in the BaseXClient.php seem to only support a successive query processing >>>>> (one result tuple after another). Or have I overseen something? If not, can >>>>> this class be quickly modified to support this? >>>>> >>>>> Many thanks for your help on this! >>>>> >>>>> >>>>> _______________________________________________ >>>>> BaseX-Talk mailing list >>>>> BaseX-Talk at mailman.uni-konstanz.de >>>>> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >>>>> >>>>> >>> >>> > > -- > Dr. Johanna Vompras > Universität Bielefeld - Universitätsbibliothek > Universitätsstr. 25, Office A2-136 > 33615 Bielefeld > > Fon +49 (0) 521/106-2644 > Fax +49 (0) 521/106-4052 > Skype johanna.vompras >

⚡Generated with XQuery