1

I have a requirement to create CSV (Comma Separated Values) files and place them in a ZIP file, using XQuery. This is easy enough to do, except that I need the CSV files in the ZIP file to be encoded with ISO-8859-1 instead of UTF-8 (because Excel expects that encoding!).

When I download a CSV file directly, I can use xdmp:set-response-encoding("ISO-8859-1") to get what I want. But is there an equivalent technique to set the encoding of a text { "..." } node that I insert into the ZIP?

Sercan
  • 4,739
  • 3
  • 17
  • 36
Neil Bradley
  • 131
  • 6
  • Are you sure that you must have that encoding? Excel should work with UTF-8 if there is a BOM https://stackoverflow.com/a/155176/14419 – Mads Hansen Jan 17 '22 at 13:45
  • 1
    You might be right, and I already tried to add a BOM within my XQuery code, but it just added those bytes as characters to the start of the output - so a way to achieve THAT might be just what I needed. But it is an interesting question even if that works for Excel. I may come up with future requirements where a non-UTF-8 encoding is definitely required. So still interested if there is a solution to that original question too! – Neil Bradley Jan 17 '22 at 14:31
  • `text()` will be maintained as UTF-8. When reading data in other encodings, it will be transcoded. Not sure if there might be some tricks to creating as a `binary()` node to put in the zip, or request the CSV from MarkLogic with the desired encoding and then perform the zip externally..... – Mads Hansen Jan 17 '22 at 22:26
  • OK, so building on that idea, I suppose I could write the text node to the file system, in a different encoding, then read it back as a binary node so the content is not modified in nany way, then add the binary to the ZIP contents, within MarkLogic. No need for an external ZIP tool if I can get that to work - I will certainly try it! – Neil Bradley Jan 18 '22 at 19:55
  • That's the best workaround I know of: write to fs, read back as binary. – asusu Jan 28 '22 at 19:22
  • That is what I did and it works. let $RandomFile := "ABCDEF.txt" let $OutputTextNode := text { $CSVData } let $Save := xdmp:save("C:\XXX\" || $RandomFile, $OutputTextNode, iso-8859-1) let $OutputBinaryNode := xdmp:document-get("C:\XXX\" || $RandomFile, binary) let $Delete := xdmp:filesystem-file-delete("C:\XXX\" || $RandomFile) return $OutputBinaryNode – Neil Bradley Feb 16 '22 at 12:09

0 Answers0