【发布时间】:2018-08-23 16:38:52
【问题描述】:
我有一个 XQuery 函数可以将一组 XML 文件转换为 HTML 并压缩它们。它在每个文件上运行一个 trasform 以创建
从那个函数开始:
declare function xport:make-sources( $path as xs:string) as item()* {
for $article in collection(xmldb:encode-uri($path))
let $docnum := $article/article/div[@class = 'content']/@doc/string()
return
<entry name="{concat($docnum,'.html')}" type='text' method='store'>
{transform:transform($article, doc("/db/EIDO/data/edit/xsl/doc-html.xsl"), <parameters/>)}
</entry>
} ;
给定输入,我运行 XQuery 来显示转换的结果......我看到了这个(正是我所期望的):
<entry name="LS01.html" type="text" method="store">
<html>
<head>
<style>
body {
font-family: Arial;
}
article img {
width:50%;
}
...
你会注意到这个条目,它们都没有 XML 声明。
但是现在让我们将它们放在一起并将这些条目发送到压缩。这一切都在 Web 应用程序中。完整的 XQuery 是这样的:
xquery version "3.0";
import module namespace transform = "http://exist-db.org/xquery/transform";
declare namespace xport = "http://www.xportability.com";
declare function xport:make-sources( $path as xs:string) as item()* {
for $article in collection(xmldb:encode-uri($path))
let $docnum := $article/article/div[@class = 'content']/@doc/string()
return
<entry name="{concat($docnum,'.html')}" type='text' method='store'>
{transform:transform($article, doc("/db/EIDO/data/edit/xsl/doc-html.xsl"), <parameters/>)}
</entry>
} ;
let $path := request:get-parameter("path", "")
let $filename := request:get-parameter("filename", "")
let $col := xport:make-sources($path)
return
response:stream-binary(
xs:base64Binary(compression:zip($col,true()) ),
'application/zip',
$filename
)
一切正常,我得到了所有已从 XML 转换为 HTML 的文档的 ZIP 文件。
但是,当我查看 ZIP 中的实际文件时,它有这个:
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
XML 声明不在 ZIP 的任何条目上。它在条目列表中的任何地方都不存在(因为它不存在)。但是压缩它们的动作显然是添加声明。我看不出其他原因或方法。即使指定 omit-xml-declaration 或将 XSL 中的输出类型更改为文本或 HTML 也没有区别。这当然是因为上面显示了 zip 的条目列表,这表明在转换之后 没有声明。
ZIP 中的文件添加了 XML 声明,句号。
有什么解决方法吗?
【问题讨论】:
-
eXist 的哪个版本?
-
您可能需要在转换后显式序列化每个文档 - 设置“omit-xml-declaration=yes”。
-
确实,
transform:transform确实采用了第五个参数,$serialization-options。见exist-db.org/exist/apps/fundocs/view.html?uri=http://…。在 XSLT 转换期间处理序列化可能比我下面的方法更直接,后者是序列化已经转换的节点。同样的原则也适用。但是,我的方法将在调用transform:transform的上下文之外起作用。两者都值得一试!