% Must be run with prova_kawa[.bat] or, otherwise, with the latest KAWA with XQuery present on CLASSPATH.
% Example XML file bib.xml should be available locally.
% This example shows
%    - to use XQuery under KAWA,
%    - how beginning with Prova 1.8, one can use multi-line constant literals. Note that newline(s) are part of the string.

:- eval(ex024()).

ex024() :-
	XQ = gnu.xquery.lang.XQuery(),
	Q = 'let $i := 2 let $r := <em>Value </em> return <p>{$r} of 10*{$i} is {10*$i}.</p>',
	println(["Query 1: ",Q]),
	X = XQ.eval(Q),
	println([X]).
ex024() :-
	XQ = gnu.xquery.lang.XQuery(),
	Q = 'children(<p>This is <em>very</em> cool.</p>)',
	println(["Query 2: ",Q]),
	X = XQ.eval(Q),
	println([X]).
ex024() :-
	XQ = gnu.xquery.lang.XQuery(),
	Q = 
'
define function descendant-or-self ($x) {
	$x,
	for $y in children($x) return descendant-or-self($y)
}
descendant-or-self(<a>X<b>Y</b></a>)
',
	println(["Query 3:",Q]," "),
	X = XQ.eval(Q),
	% Convert a Collection to a Prova list
	L = X.toArray(),
	println(L,",").
ex024() :-
	XQ = gnu.xquery.lang.XQuery(),
	Q = '<results>{ for $b in document("./bib.xml")/bib/book return $b}</results>',
	println(["Query 4: ",Q]),
	X = XQ.eval(Q),
	println([X]).
ex024() :-
	XQ = gnu.xquery.lang.XQuery(),
	PP = gnu.xml.XMLPrinter(System.out),
	Q1 = 'for $i in 1 to 5 return $i+10',
	X1 = XQ.eval(Q1),
	println([X1]),
	type(X1,T),
	println([T]),
%	PP.writeObject(X1),
%	PP.println(),
%	PP.flush().

	Q2 = '<results>{ for $b in document("./bib.xml")/bib/book where $b/publisher=" Addison-Wesley " and $b/@year>1991 return <result> { $b/title }{ $b/author }</result> }</results>',
	println([Q2]),
	XQ.eval(Q2,PP),
	PP.println(),
	PP.flush().
