我有一个xml文件目录,我想将其用作集合,具体来说,查看每个文件$my_collection/of/things
的{{1}}。
我正在使用Zorba。
这导致<things>
(语法问题?):
error retrieving resource
我还阅读了关于Zorba的Data Definition Facility的文档......对于这个单一的目的来说似乎有点过分了。或者,不是吗?
答案 0 :(得分:3)
编辑问题 - 将所有内容放入实际的Zorba集合而不是节点列表。您可能希望在您的目的中选择静态和动态集合。
<强> 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)
参考文献:
答案 1 :(得分:1)
尝试使用
collection("file:///localhost/path/to/my?select=*.xml;recurse=yes")
答案 2 :(得分:1)
用户Scala William添加的解决方案是可行的方法,但他的解决方案有点过时了。这是他为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))
};
请注意在X:get-xml-list()的返回中调用concat(...)而不是$ filename。此外,对插入节点(...)的调用已更改为在X:load-files()处插入(...)。
另外请考虑将fn:false()更改为fn:true()在fs:list(...)中的X:get-xml-list()如果您需要递归列表。