Hi Christian,
Actually, this was very interesting :)
So, I managed to create a functional XQuery:
declare variable $in external; declare variable $out external; declare variable $vendor external;
file:write-text-lines($out, 'Name,Host,Path,Count,Time'), for $x in doc($in)//record[contains(vendors,string($vendor))]
let $count := string($x/BASE) let $name := $x/name let $time := string($x/TIME_STAMP) let $path := string($x/path) let $host := string($x/host) let $result := concat ($name,',',$host,',',$path,',',$count,',',$time) let $nothing := string('')
return file:append-text-lines($out, $result)
When I test this in the editor (while setting the external variables) everything works well.
But - When I run the following command: C:\Temp>basex -b in=export_new.csv -b out=output.csv -b vendor=IID CSV_Query.xq
I get the follwing error: Stopped at C:/Temp/CSV_Query.xq, 6/15: [FODC0002] "C:/Temp/export_new.csv" (Line 1): Content is not allowed in prolog.
This seems to be failing on the basic for $x in doc($in) command.
What am I doing wrong?
Thanks, Noam
On Wed, May 27, 2015 at 10:02 PM, Christian Grün christian.gruen@gmail.com wrote:
Hi Noam,
I missed the option of adding a comma after the initial file:write
command
(the editor was constantly asking for a return command).
In XQuery, multiple expressions can always be separated with commas. For example, the following XQuery expression returns 4 items as results:
1, "string", <xml/>, file:read-text('abc.xml')
As XQuery itself, due to its functional nature, provides no guarantee that the four expressions of the above query will be evaluated one after another, the XQuery Scripting Extension [1] was proposed. It offers a semicolon to separate expressions with side-effects (such as file functions). Due to its complexity, however, it was not implemented by many XQuery implementations.
At least in BaseX (and I think in Saxon, eXist-db and Zorba as well), you can be assured that expressions separated with commas will always be evaluated one after another.
Well, this was probably more than you were asking for, but maybe it's of some interest anyway ;)
Christian
[1] http://www.w3.org/TR/xquery-sx-10/
Thanks again. It worked perfectly (although I must admit I used the dirty option, as the CSV examples are mainly on adapting CSV into XML, while I need the other direction).
Noam
On Wed, May 27, 2015 at 9:40 PM, Christian Grün <
christian.gruen@gmail.com>
wrote:
Hi Noam,
let $csv := csv:serialize($result) return file:write-text($out, $csv)
The CVS that comes out only includes one line [...]
As there are unlimited ways to represent XML nodes as CSV, there is no way to automatically a representation that always works best. For more information on creating an XML representation that will yield good results as CSV, please check out the documentation on our CSV Module [1].
Now this works, but I can't seem to find a way to add the headers to
the
first line of the file.
Obviously, I would recommend you to use the existing CSV features, because it will take care of all the usal nifty details. However, here is one simple way to let your file start with a header line:
file:write-text-lines($out, 'Name,Host,Path,Count,Time'), let $result := concat ($name,',',$host,',',$path,',',$count,',',$time) return file:append-text-lines($out, $result)
Hope this helps, Christian