Skip to content

Instantly share code, notes, and snippets.

@holmesw
Last active April 1, 2024 20:43
Show Gist options
  • Save holmesw/92b124fd4ff07b99d5200b3545375a80 to your computer and use it in GitHub Desktop.
Save holmesw/92b124fd4ff07b99d5200b3545375a80 to your computer and use it in GitHub Desktop.
XQuery 3.0 and XSLT 2.0 Library with functions to output data as CSV

XQuery and XSLT CSV Library

This XQuery 3.0 library module provides functions to output data in CSV format. CSV (Comma-Separated Values) is a common format for tabular data.

Overview

Functions

csv:value

  • Description: Outputs a value in quotes and escapes double quotes.
  • Since: version 1.0-0
  • Parameters:
    • $value: The string to output
  • Return Value: Value to use in .csv format

display-values

  • Description: Overloaded proxy function for fn:string-join, joins values in a list with a specified separator.
  • Since: version 1.0-0
  • Parameters:
    • $values: The values to output
  • Return Value: Values in joined list format

display-values

  • Description: Proxy function for fn:string-join, joins values in a list with a specified separator.
  • Since: version 1.0-0
  • Parameters:
    • $values: The values to output
    • $separator: The list value separator
  • Return Value: Values in joined list format

string

  • Description: Outputs values as strings.
  • Since: version 1.0-0
  • Parameters:
    • $values: The values to output
  • Return Value: Values as strings

XSLT Companion

This module also includes an XSLT stylesheet for CSV conversion. It includes similar functions for processing data within XSLT.

For more details on usage and implementation, refer to the provided XQuery and XSLT code.

(:~
: XQuery 3.0 library module contains functions to
: output data in CSV format
:
: @see http://en.wikipedia.org/wiki/Comma-separated_values
:
: @author holmesw
: @date 2016-06-14
: @version 1.0-0
:)
xquery version "3.0";
module namespace csv =
"http://www.example.org/xquery/csv";
declare default function namespace
"http://www.example.org/xquery/csv";
(:~
: declare common string literals as variables for use later
:
: @since version 1.0-0
:)
declare variable $csv:comma as xs:string := ",";
declare variable $csv:pipe as xs:string := "|";
declare variable $csv:new-line as xs:string := "
";
declare variable $csv:quote as xs:string := """;
(:~
: output value in quotes and escape double quotes
:
: @author holmesw
: @since version 1.0-0
:
: @see http://www.w3.org/TR/xquery-30/#id-string-concat-expr
:
: @param $value the string to output
: @return value to use in .csv
:)
declare function csv(
$value as xs:anyAtomicType
) as xs:string?
{
fn:normalize-space(
$csv:quote ||
(
if (fn:contains(string($value), $csv:quote)) then
fn:replace(string($value), $csv:quote, $csv:quote || $csv:quote)
else string($value)
) ||
$csv:quote
)
};
(:~
: overloaded proxy function for fn:string-join
:
: @author holmesw
: @since version 1.0-0
:
: @see http://www.w3.org/TR/2002/WD-xquery-operators-20021115/#func-string-join
:
: @param $values the values to output
: @return values in joined a list
:)
declare function display-values(
$values as xs:anyAtomicType*
) as xs:string?
{
display-values($values, $csv:pipe)
};
(:~
: proxy function for fn:string-join
:
: @author holmesw
: @since version 1.0-0
:
: @see http://www.w3.org/TR/2002/WD-xquery-operators-20021115/#func-string-join
:
: @param $values the values to output
: @param $separator the list value separator
: @return values in joined a list
:)
declare function display-values(
$values as xs:anyAtomicType*,
$separator as xs:string?
) as xs:string?
{
fn:string-join(string($values), $separator)
};
(:~
: output value as a string
:
: @author holmesw
: @since version 1.0-0
:
: @see http://www.w3.org/TR/2002/WD-xquery-operators-20021115/#func-string
:
: @param $values the values to output
: @return values as strings
:)
declare %private function string(
$values as xs:anyAtomicType*
) as xs:string*
{
for $v in $values return fn:string($v)
};
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:csv="http://www.example.org/xslt/csv"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="csv xs fn"
version="2.0">
<xsl:param required="no" name="comma" as="xs:string" select="','"/>
<xsl:param required="no" name="quote" as="xs:string">"</xsl:param>
<xsl:param required="no" name="pipe" as="xs:string" select="'|'"/>
<xsl:param required="no" name="new-line" as="xs:string" select="'&#10;'"/>
<xsl:function name="csv:csv" as="xs:string?">
<xsl:param name="value" as="xs:anyAtomicType" />
<xsl:value-of select="fn:normalize-space(fn:concat($quote,
fn:replace(csv:string($value), $quote,
fn:concat($quote, $quote)), $quote))" />
</xsl:function>
<xsl:function name="csv:display-values" as="xs:string?">
<xsl:param name="values" as="xs:anyAtomicType*" />
<xsl:value-of select="csv:display-values($values, $pipe)" />
</xsl:function>
<xsl:function name="csv:display-values" as="xs:string?">
<xsl:param name="values" as="xs:anyAtomicType*" />
<xsl:param name="separator" as="xs:string?" />
<xsl:value-of select="fn:string-join(csv:string($values), $separator)" />
</xsl:function>
<xsl:function name="csv:string" as="xs:string*">
<xsl:param name="values" as="xs:anyAtomicType*" />
<xsl:for-each select="$values">
<xsl:value-of select="fn:string(.)" />
</xsl:for-each>
</xsl:function>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment