代码之家  ›  专栏  ›  技术社区  ›  jbrehr

exist db/xquery压缩:xml文件的zip()仅保存文本

  •  1
  • jbrehr  · 技术社区  · 7 年前

    在现有的DB4.4、XQuery3.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
    };
    

    上面调用了这个函数,其中文档被序列化并放入 <entry> documentation :

    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 回复  |  直到 7 年前
        1
  •  2
  •   Joe Wicentowski    7 年前

    这里的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 .

    推荐文章