Warm tip: This article is reproduced from stackoverflow.com, please click
marklogic

MarkLogic unable to assign variable to cts:document-query()

发布于 2020-04-15 10:33:01

How can I assign a variable to cts:document-query(). I have sample code to read from file system then assign variable to document-query(). But it spool out errors.

case 1) working without assign variable to document-query.

let $list-pdf := cts:search(/,cts:and-query((
                          cts:directory-query("/pdf/"),
                          cts:document-query(("/pdf/US1610547.pdf", "/pdf/US1696102.pdf", 
                         "/pdf/US1953345.pdf"))) 
 for $pdf in $list-pdf 
return base-uri($pdf)

return result:

 /pdf/US1610547.pdf
 /pdf/US1696102.pdf

Case 2 - I assign variable to document-query() after reading from file system. MarkLogic gives me errors.

let $pdf := xdmp:filesystem-file("/output/listpdf.txt")
let $pdfs := tokenize($pdf,"\n")
let $list-pdf := cts:search(/,cts:and-query((
                          cts:directory-query("/pdf/"),
                          cts:document-query(($pdfs))
                                                                    ))) 
for $pdf in $list-pdf 
return base-uri($pdf)

return errorcode:

[1.0-ml] XDMP-URI: cts:document-query(("/pdf/US1610547.pdf", "/pdf/US1696102.pdf",...)) -- Invalid URI format: ""
Questioner
thichxai
Viewed
75
wst 2020-02-04 05:27

The error message indicates that the result of tokenization includes an empty string item among the sequence of URIs:

-- Invalid URI format: ""

You can apply a predicate after tokenizing to exclude empty strings (or use more elaborate logic, depending on how much your input can be trusted), i.e.:

let $pdfs := tokenize($pdf,"\n")[. ne ""]