Search code examples
markdownxquery

xquery output as markdown table


I know how to output x-query results in HTML format, see e.g. XQuery to HTML table: generating columns

But I could not find a single example on the internet to output in markdown table. (Xquery version 1.0 preferred)


id | lname | fname
===| ===== | =====
32 | Doe   | John
43 | Row   | Jane

I do not want the output as HTML to be converted to markdown, nor using any script (e.g Bash).

So far, I have been able to create the rows for the markdown table, but not the header of the table


for $i in doc("source.xml")//employees
return
string-join(($i/id, $i/lname, $i/fname), "|")

I tried to follow the instructions on this link, but I failed. XQuery - is it possible to generate non-XML document?


Solution

  • So somehow along the lines of

    declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
    
    declare option output:method "text";
    
    string-join(
      (
        string-join(('id', 'last name', 'first name'), '|'),
        string-join(('--', '--', '--'), '|'),
        for $emp in //employee return string-join(($emp/id, $emp/lname, $emp/fname), '|')
      ),
      '
'
    )