【问题标题】:MarkLogic generating self closing tag in XMLMarkLogic 在 XML 中生成自闭合标签
【发布时间】:2015-09-04 09:41:23
【问题描述】:

我正在尝试将 HTML 存储为带有空标签的 XML 格式:

    <body xmlns="http://www.w3.org/1999/xhtml">
        <div class="WordSection1" xmlns="">
            <p class="ChapterNumber">Chapter 6</p>
            <h1 class="ChapterTitle">The Legislature and the <br/>Electoral System</h1>
            <p class="ChapterSub-Title"> </p>
            <div style=""></div>
        </div>
    </body>

在 MarkLogic 中存储后,我得到空的自闭合标签

    <body xmlns="http://www.w3.org/1999/xhtml">
        <div class="WordSection1" xmlns="">
            <p class="ChapterNumber">Chapter 6</p>
            <h1 class="ChapterTitle">The Legislature and the <br />Electoral System</h1>
            <p class="ChapterSub-Title"> </p>
            <div style="" />
        </div>
    </body>

它生成了无效的 XHTML。如何获取原始 XML,无论是自闭合元素还是空元素,因为它是原始文件?

【问题讨论】:

  • 它们在语义上是相同的,为什么会出现这样的问题?
  • 感谢您的即时回复,您的意思是“语义相同”?
  • 就 XML 规范而言意思相同。
  • 谢谢 Sobrique,我找到了问题,xmlns="",空的命名空间更改为无效的 xhtml
  • 很高兴您发现了这个问题。只是为了清楚起见 - MarkLogic 不存储您的 XML。它以内部格式存储文档——本质上是逐个节点。返回的内容是即时生成的,并不一定代表“确切”您开始摄取的内容。

标签: xml marklogic


【解决方案1】:

正如上面其他人所指出的,这是一个序列化问题。 xdmp:output 选项是您的朋友。 (另请查看XQuery and XSLT serialization spec。)HTML 序列化是一种特殊情况。

declare option xdmp:output "method=html";
xdmp:set-response-content-type("text/html"),
'<!DOCTYPE html>',
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Untitled</title>
<script type="text/javascript">
 if(1 &lt; 2 &amp;&amp; 3 &lt; 4) {{
   alert("&amp;");
 }}
</script>
</head>
<body>
  <h2>Empty textarea</h2>
  <textarea></textarea>
  <h2>Empty div</h2>
  <div></div>
</body>
</html>

结果

<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta
charset="utf-8"><title>Untitled</title><script type="text/javascript">
if(1 < 2 && 3 < 4) {
alert("&");
}
</script></head><body><h2>Empty textarea</h2><textarea></textarea><h2>Empty
div</h2><div></div></body></html>

没有你得到的输出选项

<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"/><title>Untitled</title><script
type="text/javascript">
if(1 &lt; 2 &amp;&amp; 3 &lt; 4) {
alert("&amp;");
}
</script></head><body><h2>Empty textarea</h2><textarea/><h2>Empty
div</h2><div/></body></html>

请注意,此示例还演示了如何序列化 DOCTYPE(它不是 XML 数据模型的一部分)以及如何设置 HTTP 内容类型,例如,当直接从 MarkLogic 应用服务器提供 HTML 时到浏览器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多