首页 > 解决方案 > 通过包含在 MarkLogic 中包含 XQuery 结果

问题描述

我们可以通过任何方式将 XQuery 结果包含在 XML 文档中,就像我们包含其他文档或 XPath 一样。

例如:

xquery version "1.0-ml";
declare namespace xi="http://www.w3.org/2001/XInclude";

xdmp:document-insert("/test1.xml", <document>
  <p>This is a sample document.</p>
  <xi:include href="test2.xml"/> 
</document>);

xquery version "1.0-ml";
import module namespace xinc="http://marklogic.com/xinclude" at "/MarkLogic/xinclude/xinclude.xqy"; 

xinc:node-expand(fn:doc("/test1.xml"))

可以这样做如下:

xquery version "1.0-ml";
declare namespace xi="http://www.w3.org/2001/XInclude";

xdmp:document-insert("/test1.xml", <document>
  <p>This is a sample document.</p>
  <xi:include href="test2.xqy?var1=Hello&var2=world/> 
</document>);

xquery version "1.0-ml";
import module namespace xinc="http://marklogic.com/xinclude" at "/MarkLogic/xinclude/xinclude.xqy"; 

xinc:node-expand(fn:doc("/test1.xml"))

对此的任何帮助将不胜感激。

标签: nosqlxquerymarklogicmarklogic-8

解决方案


Within XQuery, an XML literal can include the result of an inline expression.

Such expressions can include a call to xdmp:invoke().

As a result, code similar the following untested example should work (replacing the MarkLogic 9 syntax for the external variables map with the MarkLogic 8 equivalent if you're on MarkLogic 8):

let $doc :=
    <document>
        <p>This is a sample document.</p>
        {xdmp:invoke("test2.xqy",
            map:entry("var1","Hello")
            => map:with("var2", "world")
            )}
    </document>
return (
    xdmp:document-insert("/test1.xml", $doc),
    $doc
    )

By the way, it's more efficient to return the inserted document from the same transaction than to read the document in a separate transaction. If the insertion fails, the transaction won't return the document, so reading in a separate transaction provides no greater assurance.

Hoping that helps,


推荐阅读