环境:eXist-db 4.2.1,XQuery 3.1
我在eXist中构建了一些函数,这些函数可以单独输出HTML结果作为函数(当我直接传递参数时)。例如,这个小功能(document:doc-sidebar-sub4
中的/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
};
产生一些来自文档foo-doc_0099.xml
的HTML片段(通过调用另一个函数):
<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]
我试图按照演示,各种教程和答案进行解答,包括this,但似乎无法找出问题所在。
另一个版本,不同的错误。这个电话
<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添加了调整项,并返回了其他错误
答案 0 :(得分:2)
通过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的模板文档中有关“模板函数”的部分说明了这两个必需参数的用途。
答案 1 :(得分:0)
在此处输入完整答案,以供将来搜索引擎编制索引。产生的错误是:
要解决此问题,该函数应如下所示:
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
};
模板调用非常简单(如果需要,可包含参数):
<div data-template="document:doc-sidebar-sub4" data-template-currentdoc="foo-doc.xml"/>
但是要使其正常工作,需要将模块导入模块view.xql
:
import module namespace document="/db/apps/fooapp/modules/document" at "/db/apps/fooapp/modules/document.xql";