简体   繁体   中英

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.

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 .

  2. Created a path range index of type int with path as /sa:root/sa:node/sa:ID

3.Trying to fetch the maximum value from the database by using the below code-

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() . See: http://docs.marklogic.com/cts:path-reference

By the way, cts:max() will probably be the most efficient way to get the maximum value from a range index. See: http://docs.marklogic.com/cts:max

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.

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.

Small change in your query is in declare namespace statement, prefix will be sa not es .

hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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