环境: eXist-db 4.2.1 , XQuery 3.1
我在 eXist 中构建了许多函数,它们将 HTML 结果作为自己的函数输出得很好(当我直接传递参数时(。举个例子,这个小函数(/modules/document.xql
document:doc-sidebar-sub4
(:
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]
我试图按照演示、各种教程和答案进行操作,但似乎无法确定问题所在。
另一个版本,不同的错误。此电话
<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添加调整,返回其他错误
通过 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 的模板化文档中有关"模板化函数"的部分解释了这两个必需参数的用途。
在这里为将来的搜索引擎索引提供完整的答案。产生的错误是:
- exerr:错误 XPTY0004:参数 3 的实际基数与函数签名中声明的基数不匹配
- err:FODC0005 exerr:错误文档.....是二进制资源,而不是 XML 文档。 模板
- :未找到 找不到用于调用的模板函数
为了解决这个问题,函数看起来像这样:
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";