这里的exist查询演示了如何将XML文档压缩到zip文件中并将其存储到数据库中:
xquery version "3.1";
(: create a test collection with 10 test files: 1.xml = <x>1</x>
thru 10.xml = <x>10</x> :)
let $prepare := xmldb:create-collection("/db", "test")
let $populate := (1 to 10) ! xmldb:store("/db/test", . || ".xml", <x>{.}</x>)
(: construct zip-bound <entry> elements for the documents in the test collection :)
let $entries := collection("/db/test") !
<entry name="{util:document-name(.)}" type="xml" method="store">{
serialize(., map { "method": "xml" })
}</entry>
(: compress the entries and store in database :)
let $zip := compression:zip($entries, false())
return
xmldb:store("/db", "test.zip", $zip)
生成的zip文件包含10个完整的测试XML文档。有关如何将zip文件写入文件系统中某个位置的变体,请参阅
https://gist.github.com/joewiz/aa8d84500b1f1478779cdf2cc1934348
.
要更全面地讨论现有的序列化选项,请参阅我对前面一个问题的回答:
https://stackoverflow.com/a/49290616/659732
.