Search code examples
collectionsxqueryzorba

Zorba collections: a simple directory of xml files


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?


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: