我有一个 xml 文件的目录,我想用作集合,特别是查看每个文件<things>
$my_collection/of/things
。
我正在使用佐巴。
这会导致error retrieving resource
(语法问题?
variable $my_collection := fn:collection("file://localhost/path/to/my/*.xml");
我还阅读了有关 Zorba 数据定义工具的文档......对于这个单一的目的来说,似乎矫枉过正。 或者,不是?
编辑问题 - 将所有内容放入实际的 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)
引用:
- http://www.zorba.io/documentation/2.9/zorba/xqddf.html
- http://en.wikipedia.org/wiki/Zorba_(XQuery_processor(
- http://www.w3.org/TR/xpath-functions-30/#func-ends-with
- http://www.zorba.io/documentation/2.9/modules/expath.org_ns_file.html
- 创建集合,使用命令行将文档添加到 zorba?
尝试使用
collection("file:///localhost/path/to/my?select=*.xml;recurse=yes")
用户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))
};
请注意对 concat(...( 的调用,而不仅仅是在 X:get-xml-list(( 的返回中$filename。此外,对 insert-nodes(...( 的调用在 X:load-files(( 处已更改为 insert(...(。
另外,如果您需要列表递归,请考虑在 X:get-xml-list(( 的 fs:list(...( 中将 fn:false(( 更改为 fn:true((。