【问题标题】:Handling undefined number of external variables处理未定义数量的外部变量
【发布时间】:2019-03-27 10:00:38
【问题描述】:

我在 REST 环境中使用 baseX,但在尝试运行具有未定义数量的 GET 变量(可能是 1,但可能是 10)的 .xq 脚本时,我非常卡住 我想让我的 xq 脚本对此通用并独立构建我的查询。 有没有办法实现这一点,玩数组或发送不同的变量,或者我不知道怎么做?


这是我的 API 调用

http://basex:8984/rest/?run=WEB-INF/data/test.xq&$tag=p&value=sciences&tag2=p&value2=test&tag3=testdzq

这是我的text.xq

declare variable $tag external;
declare variable $value external;

declare variable $tag2 external;
declare variable $value2 external;

<documents>
  {for $doc in collection("testdb2")
    where $doc//*[name() eq $tag]/text()[matches(., $value )]
      and  $doc//*[name() eq $tag2]/text()[matches(., $value2 )]

    return <doc>{$doc//titleStmt/title/text()}</doc>
  }
</documents>

谢谢!

【问题讨论】:

  • 您可以查看请求模块文档中的parameter functions
  • 谢谢!确实,找到了相关的东西:)

标签: xquery basex flwor


【解决方案1】:

在这里找到这个(见 http-params 函数)https://www.balisage.net/Proceedings/vol18/print/Murray01/BalisageVol18-Murray01.html

(: BaseX example :)

(: In the controller ... :)
module namespace c = "http://balisage.net/ns/Bal2016murr0319/controller";
import module namespace request = "http://exquery.org/ns/request";
import module namespace item = "http://balisage.net/ns/Bal2016murr0319/views/item" at "views/item.xqm";

(:~ Returns a map containing the HTTP request parameters. :)
declare function c:http-params()
as map(*)
{
  map:merge(
    for $name in request:parameter-names()
    let $value := request:parameter($name)
    return map:entry($name, $value)
  )
};

(:~ Calls the appropriate view, based on user input. :)
declare function c:get-view()
as element(html)
{
  (: get HTTP request parameters :)
  let $params := c:http-params()
  return
    if (map:get($params, "id")) then
      (: the presence of "id" indicates that the user is requesting the item-level page for this unique identifier :)
      (: call the item-level view :)
      item:get-html($params)
    else if ... (: call some other view :)
    else if ... (: call some other view :)
    else (: call the view for the home page ... :)
};

【讨论】:

    猜你喜欢
    • 2017-02-16
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2023-03-06
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多