3

My goal is to generate a CSV file out of XML using Saxon. When running the (simplified!) xquery below in Saxon (PE, 9.7.0.15), in the result for each line after the first result line, an additional space is added:

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method "text";

let $document := <A>
                    <B><C>1</C><D>2</D></B>
                    <B><C>3</C><D>4</D></B>
                    <B><C>5</C><D>6</D></B>
                </A>

for $b in $document/B
return string-join( for $x in $b/* return $x, "," ) || "&#xa;"

the result:

1,2
 3,4
 5,6

I just cannot get this additional space removed in a 'clean' way (that is: without post processing the result).

Any idea how to generate a 'clean' csv (Text) file?

DiZzZz
  • 621
  • 3
  • 12

2 Answers2

3

I think that the only thing missing was an outer string-join:

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method "text";

string-join(
  let $document := <A>
                     <B><C>1</C><D>2</D></B>
                     <B><C>3</C><D>4</D></B>
                     <B><C>5</C><D>6</D></B>
                   </A>
  for $b in $document/B
  return string-join( for $x in $b/* return $x, "," ),

  "&#xa;"
)
Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
1

For completeness, there is a second answer that does not require modifying the query.

By default, extra spaces are added between items at serialization time.

The original query will output the desired output (with no extra spaces) if you set the serialization parameter item-separator to an empty string, which will bypass the default behavior. Each engine has their own API to do so.

For Saxon, I think it would be something like passing this to the command line:

!item-separator=''

For Zorba:

-z item-separator=''
Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
  • for xquery 3.x there is a std serialization option: https://www.w3.org/TR/xslt-xquery-serialization-31/#XML_ITEM-SEPARATOR but that did not help me (should have mentioned this before, sorry) – DiZzZz May 19 '17 at 13:17