如何在MarkLogic中保留HTML5文档类型?

时间:2015-02-20 15:39:10

标签: html5 marklogic

我们需要使用Java Client API或REST API在MarkLogic中存储和检索格式良好的HTML5文档。

每个文档都有一个' .html'扩展名和标准HTML5 doctype。插入文档时,默认情况下它们将存储为文本文档。

我们希望利用MarkLogic为文档的搜索和操作提供的所有优点,就像它们是XHTML一样,但我们需要保留HTML5 doctype和.html扩展名以与其他工具兼容。我相信我们并不是唯一遇到这种情况的人。

我们已尝试将HTML mimetype更改为XML,但是在插入文档时,doctype将替换为XML doctype。有没有办法插入和检索格式良好的HTML5文档而不会丢失doctype?

2 个答案:

答案 0 :(得分:1)

没有本地方法可以将doctype保留在数据库中(XQuery不支持doctypes)。但是,使用某些逻辑,您可以在请求文档时添加文档类型。

例如:

declare function local:get-with-doctype(
    $document as document-node()
) as xs:string
{
    if (ends-with(xdmp:node-uri($document), '.html')
    then document { 
      text{ '<!DOCTYPE html>' }, 
      xdmp:quote($document) 
    }
    else $document
};

或者,您可以在插入文档时将文档类型解析出来并将其存储在文档属性中。然后,当请求文档时,您始终可以从属性中添加该文档。但是,如果您需要处理许多文档类型,那可能是值得的。

答案 1 :(得分:1)

在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或引用后的字符串)。

另见:

http://markmail.org/message/qmsos7np64ohyctp

相关问题