我们需要使用 Java 客户端 API 或 REST API 在 MarkLogic 中存储和检索格式良好的 HTML5 文档。
每个文档都有一个“.html”扩展名和标准 HTML5 doctype 。插入文档时,默认情况下它们会存储为文本文档。
我们希望利用 MarkLogic 提供的所有优点来搜索和操作文档,就像它们是 XHTML 一样,但我们需要保留 HTML5 文档类型和 .html 扩展名,以便与其他工具兼容。我相信我们不是唯一遇到过这种情况的人。
我们尝试将 HTML mimetype 更改为 XML,但是当插入文档时,doctype 会被 XML doctype 替换。有没有办法插入和检索格式良好的 HTML5 文档而不丢失文档类型?
最佳答案
稍微扩展一下 WST 的答案,您可以将文档存储为 XHTML,并使用 REST API 转换进行转换
- XQuery 转换中的 xdmp:quote() 函数,
- XSLT 转换中的 xsl:output 语句,或
- MarkLogic 8 中 JavaScript 转换中的 xdmp.quote() 函数。
REST API 的可能 XQuery 转换:
xquery version "1.0-ml";
module namespace html5ifier =
"http://marklogic.com/rest-api/transform/html5ifier";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare option xdmp:mapping "false";
declare function html5ifier:transform(
$context as map:map,
$params as map:map,
$content as document-node()
) as document-node()
{
map:put($context,"output-type","text/html"),
document{text{
xdmp:quote($content,
<options xmlns="xdmp:quote">
<method>html</method>
<media-type>text/html</media-type>
<doctype-public>html</doctype-public>
</options>)
}}
};
如果您的 REST 服务器位于端口 8011,您将使用 PUT 请求安装转换:
http://localhost:8011/v1/config/transforms/html5ifier
然后,您可以使用转换将持久的 XHTML 文档获取为 HTML5
http://localhost:8011/v1/documents?uri=/path/to/the/doc.xhtml \ &transform=html5ifier
您可以在转换中对 XHTML 文档进行其他更改(在引用之前的 XML 上或在引用之后的字符串上)。
另请参阅:
关于html - 如何在 MarkLogic 中保留 HTML5 文档类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632737/