首页 > 解决方案 > eXist-db - 包含模板导致基数不匹配,拒绝作为二进制资源

问题描述

环境:eXist-db 4.2.1,XQuery 3.1

我在 eXist 中构建了一个数字函数,它们可以单独输出 HTML 结果作为函数(当我直接传递参数时)。例如,这个小函数(document:doc-sidebar-sub4in /modules/document.xql):

module namespace document="/db/apps/fooapp/modules/document";
declare namespace templates="http://exist-db.org/xquery/templates";


import module namespace common="/db/apps/fooapp/modules/common" at "/db/apps/fooapp/modules/common.xql";

declare function document:doc-sidebar-sub4(
$node as node(), 
$model as map(*), 
$currentdoc as xs:string)

{
    let $sidebar :=
        (<div class="sidebar-sub-4">
                <p><span class="en">To cite this document:</span></p>
                <p>{common:cite-doc($currentdoc,"html")}</p>
        </div>)
    return $sidebar                 
 };

生成从文档派生的一小段 HTML foo-doc_0099.xml(通过调用另一个函数):

<div class="sidebar-sub-4">
 <p><span class="en">To cite this document:</span></p>
 <p><span>Joe Smith. <i>Digital Edition of some old manuscript</i>. 
 http://www.foosite.fr/foo-doc_0099.xml. Retrieved 2018-10-14.</span> 
 </p>
</div>

我现在正在尝试将此片段包含在我的主document.html文件中(在参数中硬编码文档 - 最终从 HTTP 请求中获取)div

<div data-template="templates:include" 
      data-template-with="document:doc-sidebar-sub4" 
      data-template-currentdoc="foo-doc_0099.xml"/>

但它会产生以下错误:

exerr:ERROR XPTY0004: The actual cardinality for parameter 3 does not match the cardinality declared in the function's signature: templates:include($node as node(), $model as map, $path as xs:string) item()*. Expected cardinality: exactly one, got 0. [at line 219, column 14, source: /Users/foo/Library/Application Support/org.exist/expathrepo/shared-0.4.2/content/templates.xql]


同一模板调用的另一个版本会产生不同的错误:

<div data-template="templates:include" 
        data-template-with="document:doc-sidebar-sub4" 
        data-template-path="modules/document.xql"
        data-template-currentdoc="foo-doc_0099.xml"/>

错误:

err:FODC0005 exerr:ERROR Document /db/apps/fooapp/modules/document.xql is a binary resource, not an XML document. Please consider using the function util:binary-doc() to retrieve a reference to it. [at line 436, column 27, source: /Users/foou/Library/Application Support/org.exist/expathrepo/shared-0.4.2/content/templates.xql]

我尝试关注演示、各种教程和答案,包括这个,但似乎无法确定问题所在。


另一个版本,不同的错误。这个电话

<div data-template="document:doc-sidebar-sub4" data-template-path="modules/document.xql":>

产生:

templates:NotFound No template function found for call document:doc-sidebar-sub4 [at line 189, column 85, source: /Users/foo/Library/Application Support/org.exist/expathrepo/shared-0.4.2/content/templates.xql]

非常感谢您对这位学习者的任何帮助。

编辑:添加来自@joewiz 的调整,返回其他错误

标签: xqueryexist-db

解决方案


您通过 eXist 的 HTML 模板工具调用的函数必须有两个必需的参数:$node as node(), $model as map(*),在任何命名参数(如您的$currentdoc参数)之前。要解决您的问题,您需要将这两个参数添加到您的函数签名中,如下所示:

declare function document:doc-sidebar-sub4(
    $node as node(), 
    $model as map(*), 
    $currentdoc as xs:string
)
{
    let $sidebar :=
        (<div class="sidebar-sub-4">
                <p><span class="en">To cite this document:</span></p>
                <p>{common:cite-doc($currentdoc,"html")}</p>
        </div>)
    return $sidebar                 
};

https://exist-db.org/exist/apps/doc/templating#D3.19的模板文档中关于“模板函数”的部分解释了这两个必需参数的用途。


推荐阅读