Hi,
Could you, please, tell what can be wrong here.
This query works:
db:open("IntersectPhoneNumbers","Data/rent_flats.csv")/csv/record/UserPhone
With result:
<UserPhone>2218520</UserPhone> <UserPhone>2219391</UserPhone> <UserPhone>2348039</UserPhone> ...
I can access the file directly from the query, without adding it into a database. This also does work, giving the same result:
let $csv := file:read-text(fn:resolve-uri('Data/rent_flats.csv')) let $rent_flats := csv:parse($csv, { 'header': true(), 'separator': 'tab' }) return $rent_flats/csv/record/UserPhone
But this does not work:
doc("Data/rent_flats.csv")/csv/record/UserPhone
Error: [FODC0002] "C:/Projects/IntersectPhoneNumbers/Data/rent_flats.csv" (Line 1): Content is not allowed in prolog.
Though I can browse the file from BaseX GUI. It is in DB. Selecting nodes in Folder Pane for that file, shows the actual data in Result Pane.
I can also access all records from all files in Data folder, including rent_flats.csv:
/csv/record/UserPhone
But I wish to refer to rent_flats.csv only.
Why is this error? I am using BaseX 7.9 on Windows XP SP3.
-- Roman
Hello Roman,
So I guess your input file is a CSV, isn't it? I would also guess you added the CSV file to the database using the CSV parse. That's when internally the same thing happened as when using csv:parse() in your second example.
As CSV is not a valid XML dialect we use a conversion to transform CSV into XML. So when loading it into the database using a CSV parser you already transformed it and it should be no problem to operate on it.
In your second example, you read the raw content and then explicitly tell the CSV parser to parse this, which also works.
Using doc() does not work, because doc searches for valid XML files, i.e. it tries to parse the file using the XML parser. As this will fail for a CSV file, you get the correct error message.
What you did in your second example using the csv:parse() is pretty much the way to go here. Without an explicit call, the processor can not decide what to do with this file (because it doesn't know it is a CSV, so you have to tell the processor).
Hope that helps, Dirk
On 09/08/14 02:53, Roman Mishin wrote:
Hi,
Could you, please, tell what can be wrong here.
This query works:
db:open("IntersectPhoneNumbers","Data/rent_flats.csv")/csv/record/UserPhone
With result:
<UserPhone>2218520</UserPhone> <UserPhone>2219391</UserPhone> <UserPhone>2348039</UserPhone> ...
I can access the file directly from the query, without adding it into a database. This also does work, giving the same result:
let $csv := file:read-text(fn:resolve-uri('Data/rent_flats.csv')) let $rent_flats := csv:parse($csv, { 'header': true(), 'separator': 'tab' }) return $rent_flats/csv/record/UserPhone
But this does not work:
doc("Data/rent_flats.csv")/csv/record/UserPhone
Error: [FODC0002] "C:/Projects/IntersectPhoneNumbers/Data/rent_flats.csv" (Line 1): Content is not allowed in prolog.
Though I can browse the file from BaseX GUI. It is in DB. Selecting nodes in Folder Pane for that file, shows the actual data in Result Pane.
I can also access all records from all files in Data folder, including rent_flats.csv:
/csv/record/UserPhone
But I wish to refer to rent_flats.csv only.
Why is this error? I am using BaseX 7.9 on Windows XP SP3.
-- Roman
Thank you Dirk. I am new to XQuery and actually learning it along with BaseX.
Yes I created a database solely for the purpose of reading CSV files (there is only 3 of them). I added these files to the database using CSV parse via GUI. The files are represented in GUI as XML-like structures. I understand they are converted to XML.
I am confusing the notion of a DB here. The documentation says that fn:doc("db/path/to/doc.xml") returns the document at the location path/to/docs in the database db. So does it actually read it from disk? Then it is understandable why BaseX complains. How should I refer to the one of these XML-like structures that I got after conversion? I can query them all at once with //UserPhone, but how to refer to only one?
-- Roman
basex-talk@mailman.uni-konstanz.de