Question

I have a directory of xml files I'd like to use as a collection, specifically, to look at $my_collection/of/things where each file has <things>.

I am using Zorba.

This results in error retrieving resource (syntax problem?):

variable $my_collection := fn:collection("file://localhost/path/to/my/*.xml");

I also read over the documentation on Zorba's Data Definition Facility... seems like overkill for this singular purpose. Or, not?

Was it helpful?

Solution

Edited question - putting everything into an actual Zorba collection as opposed to a nodelist. You might want to choose between static and dynamic collections for your purposes.

F.xq:

module namespace X = "urn:xml-files";
import module namespace ddl = "http://www.zorba-xquery.com/modules/store/static/collections/ddl";
import module namespace dml = "http://www.zorba-xquery.com/modules/store/static/collections/dml";
import module namespace file = "http://expath.org/ns/file";
import module namespace x = "http://www.zorba-xquery.com/modules/xml";
declare namespace ann = "http://www.zorba-xquery.com/annotations";

declare collection X:files as node()*;
declare variable $X:uri := xs:QName('X:files');

declare %ann:nondeterministic function X:get-xml-list($dir-name as xs:string) {
    let $files := file:list($dir-name, true())
    for $filename in $files
    where ends-with($filename, ".xml")
    return $filename
};
declare %ann:sequential function X:load-files($names as xs:string*) {
    ddl:create($X:uri);
    dml:insert-nodes($X:uri, for $filename in $names return x:parse(file:read-text($filename),()));
};
declare %ann:sequential function X:load-directory($dir-name as xs:string) {
    X:load-files(X:get-xml-list($dir-name))
};

x.xq:

xquery version "3.0" encoding "utf-8";
import module namespace X = "urn:xml-files" at "F.xq";
import module namespace dml = "http://www.zorba-xquery.com/modules/store/static/collections/dml";
X:load-directory("C:\Projects-dir...-with-xmls\");
dml:collection($X:uri)

References:

OTHER TIPS

Try to use

collection("file:///localhost/path/to/my?select=*.xml;recurse=yes")

Solution added by user Scala William is the way to go but his solution is a bit outdated. Here's his code updated for zorba 3.0:

F.xq:

module namespace X = "urn:xml-files";
import module namespace ddl = "http://zorba.io/modules/store/static/collections/ddl";
import module namespace dml = "http://zorba.io/modules/store/static/collections/dml";
import module namespace fs = "http://expath.org/ns/file";
import module namespace x = "http://zorba.io/modules/xml";
declare namespace an = "http://zorba.io/annotations";

declare collection X:files as node()*;
declare variable $X:uri := xs:QName('X:files');

declare %an:nondeterministic function X:get-xml-list($dir-name as xs:string)
{
    let $files := fs:list($dir-name, fn:false(),"*.xml")
    for $filename in $files
    return concat($dir-name,$filename)
};

declare %an:sequential function X:load-files($names as xs:string*)
{
    ddl:create($X:uri);
    dml:insert($X:uri, for $filename in $names return x:parse(fs:read-text($filename),()));
};

declare %an:sequential function X:load-directory($dir-name as xs:string)
{
    X:load-files(X:get-xml-list($dir-name))
};

Please note the call to concat(...) instead of just $filename in the return at X:get-xml-list(). Also, the call to insert-nodes(...) has changed to insert(...) at X:load-files().

Also please consider changing fn:false() to fn:true() in fs:list(...) at X:get-xml-list() if you need your listing recursive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top