简体   繁体   English

如何在MarkLogic中使用cts:values获得元素的最大值?

[英]How to get the maximum value of an element using cts:values in MarkLogic?

I want to get the Maximum value of <ID> from all the documents present inside the database. 我想从数据库中存在的所有文档中获取<ID>的最大值。

Sample Document- 样本文件

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

The approach which i tried is as below- 我尝试的方法如下-

  1. I created a path namespace with prefix sa with uri http://marklogic.com/sample . 我使用uri http://marklogic.com/sample创建了前缀为sa的路径名称空间。

  2. Created a path range index of type int with path as /sa:root/sa:node/sa:ID 创建了一个类型为int的路径范围索引,其路径为/sa:root/sa:node/sa:ID

3.Trying to fetch the maximum value from the database by using the below code- 3.尝试使用以下代码从数据库中获取最大值:

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

But this is giving me an empty sequence. 但这给了我一个空序列。 Not sure what i am missing here. 不知道我在这里想念什么。

Any Suggestions ?? 有什么建议么 ??

Try passing a map with the namespace bindings as the third argument to cts:path-reference() . 尝试将带有名称空间绑定的映射作为第三个参数传递给cts:path-reference() See: http://docs.marklogic.com/cts:path-reference 请参阅: http//docs.marklogic.com/ctspath-reference

By the way, cts:max() will probably be the most efficient way to get the maximum value from a range index. 顺便说一句, cts:max()可能是从范围索引中获取最大值的最有效方法。 See: http://docs.marklogic.com/cts:max 请参阅: http//docs.marklogic.com/ctsmax

The approach would resemble the following fragment: 该方法类似于以下片段:

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

Hoping that helps, 希望能有所帮助,

As suggested by Elijah Bernstein-Cooper I just added the xmlns="http://marklogic.com/sample" namespace in the xml shared by you and inserted few xml files in the db. Elijah Bernstein-Cooper所建议,我只是在您共享的xml中添加了xmlns="http://marklogic.com/sample"命名空间,并在数据库中插入了一些xml文件。

Created the path namespace, path range index and ran the shared cts query and it worked perfectly so Elijah is correct you just need to specify the namespace in the xml. 创建了路径名称空间,路径范围索引,并运行了共享的cts查询,它运行良好,因此Elijah是正确的,您只需要在xml中指定名称空间即可。

Small change in your query is in declare namespace statement, prefix will be sa not es . 在查询中的小变化是在declare namespace声明,前缀将saes

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM