Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to format MarkLogic results when exported to a CSV file
    primarykey
    data
    text
    <p>I have XML which is structured similar to the example below and I've written an XQuery in MarkLogic to export this to a CSV (see below the XML). </p> <p>What I need help with is formatting the output so that when I open the CSV file, instead of having all of the output across 1 I'd like it to be grouped "columns" so to speak. </p> <p>Let's say for the sample below, I'd like to output all of the DataTime and Source element values and have the values in their own columns like this:</p> <pre><code>2012-02-15T00:58:26 a 2012-02-15T00:58:26 b 2012-02-15T00:58:26 c </code></pre> <p>How would I go about that?</p> <p>Would welcome any reference points or help. Thank you in advance.</p> <p>Here's the sample XML:</p> <pre><code>&lt;Document xmlns="http://fakeexample.org/schemas"&gt; &lt;Information&gt; &lt;ItemId&gt;1f28cb0c2c4f4eb7b13c4abf998e391e&lt;/ItemId&gt; &lt;MediaType&gt;Text&lt;/MediaType&gt; &lt;DocDateTime&gt;2012-02-15T00:58:26&lt;/DocDateTime&gt; &lt;/Information&gt; &lt;FilingData&gt; &lt;DateTime&gt;2012-02-15T00:58:26&lt;/DateTime&gt; &lt;Source&gt;a&lt;/Source&gt; &lt;/FilingData&gt; &lt;FilingData&gt; &lt;DateTime&gt;2012-02-15T00:58:27&lt;/DateTime&gt; &lt;Source&gt;b&lt;/Source&gt; &lt;/FilingData&gt; &lt;FilingData&gt; &lt;DateTime&gt;2012-02-15T00:58:28&lt;/DateTime&gt; &lt;Source&gt;c&lt;/Source&gt; &lt;/FilingData&gt; &lt;/Document&gt; </code></pre> <p>Here's the sample XQuery:</p> <pre><code>xquery version "1.0-ml"; declare default function namespace "http://www.w3.org/2005/xpath-functions"; declare namespace xdmp="http://marklogic.com/xdmp"; declare namespace exam="http://fakeexample.org/schemas"; declare function local:getDocument($url) { let $response := xdmp:document-get($url, &lt;options xmlns="xdmp:document-get"&gt; &lt;repair&gt;full&lt;/repair&gt; &lt;format&gt;xml&lt;/format&gt; &lt;/options&gt;) return $response }; xdmp:set-response-content-type("text/csv"), xdmp:add-response-header( "Content-disposition", fn:concat("attachment;filename=", "output", fn:current-time(), ".csv") ), ( let $q := cts:element-value-query(xs:QName("exam:ItemId"), ("1f28cb0c2c4f4eb7b13c4abf998e391e")) let $results := cts:search(fn:doc(), $q) for $result in $results return fn:string-join((xs:string($result//exam:DateTime), xs:string($result//exam:Source) ), "," ) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload