Xquery 如何在eXist db中的函数之间共享标记片段?

Xquery 如何在eXist db中的函数之间共享标记片段?,xquery,exist-db,Xquery,Exist Db,我想知道是否有一种方法可以在eXistDB中共享html代码片段。我有两个不同的函数(稍后会有更多期望)返回相同的大html表单以获得不同的结果。当我更改其中一个代码时,维护相同的代码是很烦人的。我试过: 将其保存为html文件,并使用doc()函数(eXist)加载它,它抱怨它不是xml文件,而是二进制文件 将它像全局变量一样保存到一个单独的模块中(eXist抱怨上下文有问题)。我不知道如何在没有名称空间前缀的情况下传递这样的变量 保存它就像函数返回自己的大变量一样(eXist抱怨上下文有问题

我想知道是否有一种方法可以在eXistDB中共享html代码片段。我有两个不同的函数(稍后会有更多期望)返回相同的大html表单以获得不同的结果。当我更改其中一个代码时,维护相同的代码是很烦人的。我试过:

  • 将其保存为html文件,并使用
    doc()
    函数(eXist)加载它,它抱怨它不是xml文件,而是二进制文件
  • 将它像全局变量一样保存到一个单独的模块中(eXist抱怨上下文有问题)。我不知道如何在没有名称空间前缀的情况下传递这样的变量
  • 保存它就像函数返回自己的大变量一样(eXist抱怨上下文有问题)
  • 最佳做法是什么

    更新

    嗯,我尝试将代码段放入作为模块加载的函数中的变量InInde中。对我来说,这似乎是合理的。但是,我在尝试返回时出错:

    err:XPDY0002 Undefined context sequence for 'child::snip:snippet' [at line 62, column 13, source: /db/apps/karolinum-apps/modules/app.xql]
    In function:a pp:book-search(node(), map, xs:string?) [34:9:/db/apps/karolinum-apps/modules/app.xql]
    
    我这样称呼它:

    declare function app:list-books($node as node(), $model as map(*)) {
        for $resource in collection('/db/apps/karolinum-apps/data/mono')
        let $author := $resource//tei:titleStmt/tei:author/text()
        let $bookName := $resource//tei:titleStmt/tei:title/text()
        let $bookUri := base-uri($resource)
        let $imgPath := replace($bookUri, '[^/]*?$', '')
        let $fileUri := ( '/exist/rest' || $bookUri )
        let $fileName := replace($bookUri, '.*?/', '')
        return
            if ($resource//tei:titleStmt/tei:title)
            then
                snip:snippet
            else ()
    };
    
    有什么想法吗

    更新II

    在这里,我有模块中的功能:

    module namespace snip = "http://46.28.111.241:8081/exist/db/apps/karolinum-apps/modules/snip";
    
    declare function snip:snippet($node as node(), $model as map(*), $author as xs:string, $bookTitle as xs:string, $bookUri as xs:anyURI, $fileUri as xs:anyURI) as element()* {
        let $snippet := 
            (
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><a href="{$fileUri}">{$bookTitle}</a> ({$author})</h3>
                    </div>
                    <div class="panel-body">
                    ...
                    </div>
            )
            return $snippet
    };
    
    它抛出:

    err:XPST0017 error found while loading module app: Error while loading module app.xql: Function snip:snippet() is not defined in namespace 'http://46.28.111.241:8081/exist/db/apps/karolinum-apps/modules/snip' [at line 35, column 9]
    

    当我尝试将代码段放入变量时,无法将使用的那些局部变量传递到那里(它抛出了
    $fileUri未设置
    )。此外,我尝试更改返回的类型
    元素()*
    ,但没有任何帮助。

    所有方法都应该有效。让我逐一说明:

  • HTML代码段的XML格式是否正确?如果是,请将其另存为,例如
    form.XML
    form.HTML
    (因为默认情况下eXist假定扩展名为
    .HTML
    的文件格式正确;请参阅eXist安装文件夹中的mime-types.XML)并使用
    doc($path)引用它
    。如果格式不正确,可以将其另存为
    form.txt
    ,并使用
    util:binary-to-string(util:binary-doc($path))
    。或者使HTML格式正确并使用第一种替代方法

  • 这也是有效的,因此您一定没有正确声明或引用全局变量。您得到的确切错误是什么?您可以发布一个小示例片段,我们可以运行它来重现您的结果吗

  • 见#2


  • 你的所有方法都应该有效。让我逐一说明:

  • HTML代码段的XML格式是否正确?如果是,请将其另存为,例如
    form.XML
    form.HTML
    (因为默认情况下eXist假定扩展名为
    .HTML
    的文件格式正确;请参阅eXist安装文件夹中的mime-types.XML)并使用
    doc($path)引用它
    。如果格式不正确,可以将其另存为
    form.txt
    ,并使用
    util:binary-to-string(util:binary-doc($path))
    。或者使HTML格式正确并使用第一种替代方法

  • 这也是有效的,因此您一定没有正确声明或引用全局变量。您得到的确切错误是什么?您可以发布一个小示例片段,我们可以运行它来重现您的结果吗

  • 见#2


  • 我非常接近,必须以某种方式将参数传递给嵌套函数,并省略eXist的典型
    $node作为node(),$model作为map(*)
    作为参数

    模板功能:

    declare function app:list-books($node as node(), $model as map(*)) {
        for $resource in collection('/db/apps/karolinum-apps/data/mono')
        let $author := $resource//tei:titleStmt/tei:author/text()
        let $bookTitle := $resource//tei:titleStmt/tei:title/text()
        let $bookUri := base-uri($resource)
        let $bookId := xs:integer(util:random() * 10000)
        let $fileUri := ('/exist/rest' || $bookUri)
        let $fileName := replace($bookUri, '.*?/', '')
        where not(util:is-binary-doc($bookUri))
        order by $bookTitle, $author
        return
            snip:snippet($author, $bookTitle, $bookUri, $bookId, $fileUri)
    };
    
    declare function snip:snippet($author as xs:string, $bookTitle as xs:string, $bookUri as xs:anyURI, $bookId as xs:string, $fileUri as xs:anyURI) as element()* {
        let $snippet := 
            (
                <div class="panel panel-default">
                ...
               </div>
           )
       return $snippet
    };
    
    代码段函数:

    declare function app:list-books($node as node(), $model as map(*)) {
        for $resource in collection('/db/apps/karolinum-apps/data/mono')
        let $author := $resource//tei:titleStmt/tei:author/text()
        let $bookTitle := $resource//tei:titleStmt/tei:title/text()
        let $bookUri := base-uri($resource)
        let $bookId := xs:integer(util:random() * 10000)
        let $fileUri := ('/exist/rest' || $bookUri)
        let $fileName := replace($bookUri, '.*?/', '')
        where not(util:is-binary-doc($bookUri))
        order by $bookTitle, $author
        return
            snip:snippet($author, $bookTitle, $bookUri, $bookId, $fileUri)
    };
    
    declare function snip:snippet($author as xs:string, $bookTitle as xs:string, $bookUri as xs:anyURI, $bookId as xs:string, $fileUri as xs:anyURI) as element()* {
        let $snippet := 
            (
                <div class="panel panel-default">
                ...
               </div>
           )
       return $snippet
    };
    
    将函数snip:snippet($author作为xs:string,$bookTitle作为xs:string,$bookUri作为xs:anyURI,$bookId作为xs:string,$fileUri作为xs:anyURI)声明为元素(){
    让$snippet:=
    (
    ...
    )
    返回$snippet
    };
    
    我非常接近。必须以某种方式将参数传递给嵌套函数,并省略eXist的典型
    $node作为node(),$model作为map(*)作为参数

    模板功能:

    declare function app:list-books($node as node(), $model as map(*)) {
        for $resource in collection('/db/apps/karolinum-apps/data/mono')
        let $author := $resource//tei:titleStmt/tei:author/text()
        let $bookTitle := $resource//tei:titleStmt/tei:title/text()
        let $bookUri := base-uri($resource)
        let $bookId := xs:integer(util:random() * 10000)
        let $fileUri := ('/exist/rest' || $bookUri)
        let $fileName := replace($bookUri, '.*?/', '')
        where not(util:is-binary-doc($bookUri))
        order by $bookTitle, $author
        return
            snip:snippet($author, $bookTitle, $bookUri, $bookId, $fileUri)
    };
    
    declare function snip:snippet($author as xs:string, $bookTitle as xs:string, $bookUri as xs:anyURI, $bookId as xs:string, $fileUri as xs:anyURI) as element()* {
        let $snippet := 
            (
                <div class="panel panel-default">
                ...
               </div>
           )
       return $snippet
    };
    
    代码段函数:

    declare function app:list-books($node as node(), $model as map(*)) {
        for $resource in collection('/db/apps/karolinum-apps/data/mono')
        let $author := $resource//tei:titleStmt/tei:author/text()
        let $bookTitle := $resource//tei:titleStmt/tei:title/text()
        let $bookUri := base-uri($resource)
        let $bookId := xs:integer(util:random() * 10000)
        let $fileUri := ('/exist/rest' || $bookUri)
        let $fileName := replace($bookUri, '.*?/', '')
        where not(util:is-binary-doc($bookUri))
        order by $bookTitle, $author
        return
            snip:snippet($author, $bookTitle, $bookUri, $bookId, $fileUri)
    };
    
    declare function snip:snippet($author as xs:string, $bookTitle as xs:string, $bookUri as xs:anyURI, $bookId as xs:string, $fileUri as xs:anyURI) as element()* {
        let $snippet := 
            (
                <div class="panel panel-default">
                ...
               </div>
           )
       return $snippet
    };
    
    将函数snip:snippet($author作为xs:string,$bookTitle作为xs:string,$bookUri作为xs:anyURI,$bookId作为xs:string,$fileUri作为xs:anyURI)声明为元素(){
    让$snippet:=
    (
    ...
    )
    返回$snippet
    };
    
    非常感谢。一般来说,我指的是
    ..
    div
    中,有一个完整的大表单。我不确定它是否是严格有效的XML(eXist不抱怨)。但它使用了几个变量。函数根本不返回它(现在)。啊,是的,这些变量不是活动的(像文本一样传递)。如果eXist没有抱怨,那么它就是有效的XML。但不要期望“变量”(我认为您的意思是XQuery)起作用如果存储在.xml或.html文件中,则此类文件将被视为静态文档,而不像.xq文件那样进行计算。如果您需要表单是动态的,则肯定需要使用方法#2或#3。或者,第四个选项:使用eXist的模板功能:。这可能非常适合您的任务。嗯,我使用模板,但不知道它是否正确可以嵌套函数。在模板中,我调用
    app:list books
    ,结果是一个图书列表,每本书都有一个表单。是否可以在另一个模板函数中使用模板函数调用?非常感谢。一般来说,我指的是
    div
    中的
    ,有整个大表单。我不确定它是否是严格有效的XML(eXist不抱怨)。但它使用了几个变量。函数只是不返回它(现在)。啊,是的,这些变量不是活动的(像文本一样传递)。如果eXist不抱怨,那么它就是有效的XML。但不要期望“变量”(我认为你的意思是XQuery)会“工作”如果存储在.xml或.html文件中;这类文件被视为静态文档,而不像.xq文件那样进行计算。如果您需要表单是动态的,您肯定需要使用方法#2或#3。或者,第四个选项:使用eXist的模板工具:。这可能非常适合