Changes

Jump to navigation Jump to search
119 bytes added ,  14:54, 27 February 2020
no edit summary
* <code>$csv?names?*</code> returns the names of all fields (if available)
* Return enumerated strings for all records:
<pre classsyntaxhighlight lang="brush:xquery">
for $record at $pos in $csv?records
return $pos || ". " || string-join($record?*, ', ')
</presyntaxhighlight>
The resulting representation consumes less memory than XML-based formats, and values can be directly accessed without conversion. Thus, it is recommendable for very large inputs and for efficient ad-hoc processing.
'''Input''' {{Code|addressbook.csv}}:
<pre classsyntaxhighlight lang="brush:xml">
Name,First Name,Address,City
Huber,Sepp,Hauptstraße 13,93547 Hintertupfing
</presyntaxhighlight>
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $text := file:read-text('addressbook.csv')
return csv:parse($text, map { 'header': true() })
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<csv>
<record>
</record>
</csv>
</presyntaxhighlight>
'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $options := map { 'lax': false() }
let $input := file:read-text('some-data.csv')
let $output := $input => csv:parse($options) => csv:serialize($options)
return $input eq $output
</presyntaxhighlight>
'''Example 3:''' Converts CSV data to XQuery and returns distinct column values:
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $text := ``[Name,City
Jack,Chicago
)
)
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xquery">
Distinct values:
* Name: Jack, John
* City: Chicago, Washington, New York
</presyntaxhighlight>
=Errors=
Bureaucrats, editor, reviewer, Administrators
12,235

edits

Navigation menu