Reading in an external text file

378 views
Skip to first unread message

Nick Sincaglia

unread,
Jan 18, 2015, 5:04:43 PM1/18/15
to zorba...@googlegroups.com
I have an external text file which I would like to pass in to my Xquery script.

I am unclear on what I need to do get Zorba to read in the file. I have been looking at the predefined file functions here:
http://www.zorba.io/documentation/latest/modules/expath/file

read-binary()
read-text-lines()
read-text()

but nothing I have tried has gotten me past the error I am getting which is:

While binding external variable $xml-file: <eval@-4-1>:1,2 (in data 1,1): dynamic error [err:FODC0006]: invalid content passed to fn:parse-xml(): "":1,1: loader parsing error: empty XML document ('<' expected)

It is probably very simple but I have not been able to locate any sample code. Can someone one point me to an example of how a read in a text file using Zorba?

I was able to get it to work using eXist-db by calling these functions:
let $text-file := util:binary-to-string(util:binary-doc('my-text-file.txt'))

I am looking for the equivalent to this with Zorba functions.

Nick

William Candillon

unread,
Jan 19, 2015, 3:08:27 PM1/19/15
to zorba...@googlegroups.com
Hi Nick,

Could you please send me the code snippet that doesn't work for you?

Kind regards,

William
> --
> You received this message because you are subscribed to the Google Groups
> "Zorba Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to zorba-users...@googlegroups.com.
> To post to this group, send email to zorba...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/zorba-users/4ceb8472-d09c-45ee-a714-079c0be4a0aa%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nick Sincaglia

unread,
Jan 20, 2015, 7:24:08 PM1/20/15
to zorba...@googlegroups.com
I just want to do something simple like convert a pipe delimited text file into an XML file. 

I can read in an external XML file just fine but when I try to read in a pipe delimited text file, I get the error mentioning fn:parse-xml().

There must be a function that I can use to let Zorba know the file I want to read in is not XML but a binary or text file.

Below is a snippet of code that will give you an idea of what I am looking to do.

declare variable $text-file external;

let $input-text := $text-file

let $text-to-xml :=
   for $line := tokenize($input-text,'\n')
   let $element := tokenize($line,'\|')
   return
        <line>
          <field1>{$element[1]}</field1>
          <field2>{$element[2]}</field2>
          <field3>{$element[3]}</field3>
       </line>
return
    $text-to-xml

William Candillon

unread,
Jan 20, 2015, 7:32:56 PM1/20/15
to zorba...@googlegroups.com
Which part of this code snippet throw the error?
Anything that would helpful to reproduce the error would be helpful.
> https://groups.google.com/d/msgid/zorba-users/afd52a9f-a938-4446-8d05-654efd4fab75%40googlegroups.com.

William Candillon

unread,
Jan 20, 2015, 7:53:00 PM1/20/15
to zorba...@googlegroups.com
What is the command you are using?
For external variables, the cli specifies:
--external-variable, -e
Provide the value for a variable given a file (name=file) or a value
(name:=value)

Maybe I would recommend to set a string value for the filename and
read text from xquery in order to have more control.

Nick Sincaglia

unread,
Jan 21, 2015, 5:08:47 PM1/21/15
to zorba...@googlegroups.com
When I call my script from the command line, I use the following:

>zorba -e text-file=my-pipe-delimited-text-file.txt -o My_Output_XML_File.xml -f -q my-Xquery-script-file.xq

When I try to run it I get the following error.

While binding external variable $text-file: <eval@-4-1>:1,2 (in data 1,1): dynamic error [err:FODC0006]: invalid content passed to fn:parse-xml(): "":1,1: loader parsing error: empty XML document ('<' expected)

Like I said earlier, if the input file is XML, I am able to read it in. If it is a text file, it complains that "invalid content passed"

Let's simplify the script even further. What if I just reduce the program (xquery-read-text-file.xq) to this?

declare variable $text-file external;
let $input-text := $text-file

return
   
$input-text

I make my text file (sample-text-file.txt) this:

This is line 1
This is line 2
This is line 3

I call this program from my command line like this:

zorba -e text-file=sample-text-file.txt -f -q xquery-read-text-file.xq

I receive an error like this:

While binding external variable $text-file: <eval@-4-1>:1,2 (in data 1,1): dynamic error [err:FODC0006]: invalid content passed to fn:parse-xml(): "":1,1: loader parsing error: empty XML document ('<' expected)

If I were to instead, read in a XML file (sample-xml-file.xml), this script would work fine.

Is there a special function I need to use to read text files in? It sounds like, from your response, that there is nothing special I need to do to read text files in. Is that correct? Are you able to run the scenario I have outlined here?

William Candillon

unread,
Jan 21, 2015, 5:13:18 PM1/21/15
to zorba...@googlegroups.com
Can you try

zorba -e text-file:=my-pipe-delimited-text-file.txt -o
My_Output_XML_File.xml -f -q my-Xquery-script-file.xq
> https://groups.google.com/d/msgid/zorba-users/fa3ceb56-86a0-432f-bbf1-19e4866d3211%40googlegroups.com.

Nick Sincaglia

unread,
Jan 21, 2015, 11:44:42 PM1/21/15
to zorba...@googlegroups.com
I was able to get it to read a text file with this code

import module namespace file = "http://expath.org/ns/file";

declare variable $text-file external;
let $input-text := file:read-text($text-file)
return
   
$input-text

If I don't use the file:read-text() function, it just returns the name of the file that I pass in. 

I am a little confused because I thought I was experiencing something different the other day. I will need to go back and try to recreate the issue. 

Nick

Atish Mehta

unread,
Nov 15, 2016, 7:17:41 AM11/15/16
to Zorba Users
Hi,
I am facing an issue while running the below zorba command on Centos.
docker run zorba -i -r --trailing-nl -e myfile:=/swagger.json -e pathtext:=/v1/people -f swaggerProcessor.xq > People.json
It shows error:file {swaggerProcessor.xq} not found or not readable.

I checked it is there and has full rights.

Regards
Atish Mehta

William Candillon

unread,
Nov 15, 2016, 7:30:33 AM11/15/16
to zorba...@googlegroups.com
Hello Atish,

swaggerProcessor.xq sounds like a cool query ;-)
Can you try:
docker run --rm -v $(pwd):/queries zorba -i -r --trailing-nl -e
myfile:=/swagger.json -e pathtext:=/v1/people -f swaggerProcessor.xq >
People.json

Kind regards,

William
> https://groups.google.com/d/msgid/zorba-users/a4cd5f1a-4f7b-4e48-9194-9872e18fcbc0%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages