0
votes

On my website (eXist-db, Xquery 3.1) users can download dynamically-generated documents. I have successfully implemented this for .pdf, .xml, and .zip files.

Now I am trying to get .csv outputs into serializing, but failing.

I get the following error:

checking function parameter 1
The actual cardinality for parameter 1 does not match the cardinality declared in the function's signature: . Expected cardinality: zero or one, got 2.

The error is triggered in the function local:download() in the line let $binaryresult := util:string-to-binary($result,'UTF-8')

I thought I could pass a series of strings to the serializer, and then the serializer would effectively out each item into a line in the .csv document (creating a line return).

Evidently I'm approaching the serialization of .csv wrong, but haven't been able to figure out how t correct this. The problem is fundamentally "how to output data rows to a CSV with line returns".

declare option exist:serialize "method=text media-type=text/csv"; 
declare function local:output-csv()
{
  $output := (concat("title1", ",", "title2"),concat("data1a", ",", "data2a"),concat("data1b", ",", "data2b"))
  return $output
};
declare function local:download()
{
 let $filename := "search_results.csv"
 let $result := local:output-csv()
 let $binaryresult := util:string-to-binary($result,'UTF-8')
 return 
      response:stream-binary($binaryresult,'text/csv',$filename)
};

Thanks in advance for help with this.

1

1 Answers

2
votes

I am not familiar with eXist-db and I don't know whether you need a string-to-binary function at all to return a text result like CSV but I think you can use string-join($output, '
') in the local:output-csv function or use it in the other function with util:string-to-binary(string-join($result, '
'), 'UTF-8') to make sure you pass a single string to that function and not a sequence of strings.