开发者

How to load a text file from within an XQuery?

开发者 https://www.devze.com 2023-01-28 20:07 出处:网络
Is there an XQuery command to load a text file? I can load an xml document by doing the following; declare variable $text := doc(\"test.xml\");

Is there an XQuery command to load a text file?

I can load an xml document by doing the following;

declare variable $text := doc("test.xml");

But it only seems to work if test.xml is a well-formed xml document. What I want is to load a plain test.txt file into a string variable. Something like this;

declare variable $str as xs:string := fn:loadfile("test.txt");

Can it be done?

I'm using the Saxon engine but I can't find an answer in the saxon documentat开发者_运维问答ion.


Indeed you can find one implementation of the file functions in Zorba: http://www.zorba-xquery.com/doc/zorba-1.4.0/zorba/xqdoc/xhtml/www.zorba-xquery.com_modules_file.html


XQuery 3.0 has the function fn:unparsed-text (which was originally defined in XSLT), which does exactly what you want. XQuery 3.0 is still a work in progress though, but whilst there are not many XQuery 3.0 processors available, many XQuery processors already support this function (including Saxon).


There is a standardization effort for this on EXPath. A spec already exists for an XQuery File module that is capable of doing what you describe: EXPath File Module Spec.

Yet, I don't know how many implementations are out there. Saxon doesn't seem to implement it unfortunately (Or, please point me to it). An example implementation is shipped with zorba (see XQDoc Site of Zorba). If you want to know how to get started with zorba, you can check out this tutorial: Get Started with XQuery and Zorba.


XQuery by default( means fn: namespace ) doesn;t have any file-access methods.

MarkLogic : xdmp:filesystem-file() xdmp:filesystem-directory()

Zorba: already mentioned by user457056

Exist Exist File Module


Saxon since version 9.2 has an extension of fn:collection that can be used to read unparsed text. Here is an example:

    collection('file:///c:/TEMP?select=text.txt;unparsed=yes')

This is described under "Changes in this Release" for 9.2. Apparently it is not mentioned in the function library documentation. However it works well and I have been using it a lot.

0

精彩评论

暂无评论...
验证码 换一张
取 消