【问题标题】:How to get the maximum value of an element using cts:values in MarkLogic?如何使用 MarkLogic 中的 cts:values 获取元素的最大值?
【发布时间】:2018-08-30 12:14:00
【问题描述】:

我想从数据库中存在的所有文档中获取<ID> 的最大值。

示例文档-

<root xmlns="http://marklogic.com/sample">
 <node>
  <ID>3253523</ID>
  <value1>.....</value1>
  <value2>.....</value2>
  <value3>.....</value3>
  <value4>.....</value4>
   .....................
 </node>
</root>

我尝试的方法如下-

  1. 我创建了一个路径命名空间,前缀为 sa,uri http://marklogic.com/sample

  2. 创建了一个int类型的路径范围索引,路径为/sa:root/sa:node/sa:ID

3.尝试使用以下代码从数据库中获取最大值-

declare namespace sa = "http://marklogic.com/sample"; (cts:values(cts:path-reference('/sa:root/sa:node/sa:ID'), (), "descending"))[1]

但这给了我一个空序列。不知道我在这里缺少什么。

有什么建议吗??

【问题讨论】:

  • 你确定节点有命名空间http://marklogic.com/sample吗?我希望根元素看起来像&lt;root xmlns="http://marklogic.com/sample"&gt;。您的其余步骤看起来会起作用。
  • 是的,对不起,我忘了在我的示例文档中添加它。但无论你提到什么,它看起来都一样。
  • 是不是因为int值太长3253523.??

标签: xquery marklogic


【解决方案1】:

尝试将带有命名空间绑定的映射作为第三个参数传递给cts:path-reference()。见:http://docs.marklogic.com/cts:path-reference

顺便说一句,cts:max() 可能是从范围索引中获取最大值的最有效方法。见:http://docs.marklogic.com/cts:max

该方法类似于以下片段:

cts:max(
    cts:path-reference('/sa:root/sa:node/sa:ID', (),
        map:entry("sa", "http://marklogic.com/sample")
    ))

希望对您有所帮助,

【讨论】:

  • @ehennum-- 请给我一个例子,说明我应该如何在这种情况下使用cts:max()
【解决方案2】:

正如Elijah Bernstein-Cooper 所建议的那样 我刚刚在你共享的xml中添加了xmlns="http://marklogic.com/sample"命名空间,并在db中插入了几个xml文件。

创建了路径命名空间、路径范围索引并运行了共享 cts 查询,它运行良好,所以 Elijah 是正确的,您只需在 xml 中指定命名空间。

您的查询中的小变化在declare namespace 语句中,前缀将是sa 而不是es

希望这会有所帮助。

【讨论】:

  • 感谢我修改了命名空间。但不知何故,我错过了一些东西,它给了我一个空序列。
  • 能否请您提供来自 8001 端口的 cts:queryscreenshot of xml saved in the DB from qconsolescreenshot of the path-namespace and path-range-index。这将有助于我在更短的时间内为您提供帮助。
猜你喜欢
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2022-10-01
相关资源
最近更新 更多