XML文件的eXist-db / XQuery compression:zip()仅保存文本

时间:2019-01-13 13:24:10

标签: xquery exist-db

在eXist-db 4.4,XQuery 3.1中,我正在使用自动化来压缩许多xml文件。问题在于,当他们压缩时,它们仅存储文本内容,而不存储xml内容。

此功能使用compression:zip从一批文档中创建一个zip:

declare option exist:serialize "expand-xincludes=no";
declare option exist:serialize "method=xml media-type=application/xml";
declare function zip:create-zip-by-batch()
{
    [...]

    let $zipobject := compression:zip(zip:get-entry-for-zip($x,false())

    let $zipname := "foozipname.zip"

    let $store := xmldb:store("/db/foodirectory", $zipname, $zipobject)

    return $store
};

上面的代码调用此函数,在该函数中,文档被序列化并按documentation放入<entry>

declare option exist:serialize "expand-xincludes=no";
declare option exist:serialize "method=xml media-type=application/xml";
declare function zip:get-entry-for-zip($x) 
{

 [...for each $foo document in $x, create an <entry>...]

 let $serialized := serialize($foo, map { "method": "xml" })
 let $entry =
          <entry name="somefooname" type='xml' method='store'>
               {$serialized}
          </entry>

 [...return a sequence of $entry...]
 }

我认为它缺少用于序列化的配置,但我无法弄清楚...

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

这里有一个关于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

要更全面地讨论eXist中的序列化选项,请参阅我对先前问题的回答:https://stackoverflow.com/a/49290616/659732