I want to get the Maximum value of <ID> from all the documents present inside the database.

Sample Document-

<root xmlns=""> <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 .

  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 = ""; (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 ??

3

2 Answers

Try passing a map with the namespace bindings as the third argument to cts:path-reference(). See:

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

The approach would resemble the following fragment:

cts:max( cts:path-reference('/sa:root/sa:node/sa:ID', (), map:entry("sa", "") )) 

Hoping that helps,

1

As suggested by Elijah Bernstein-Cooper I just added the xmlns="" 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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.