How to prevent extra spaces when converting XML to text (csv)

My goal is to generate a CSV file from XML using Saxon. Running the (simplified!) Xquery below in Saxon (PE, 9.7.0.15) adds extra space for each line after the first line of the result:

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;"

      

result:

1,2
 3,4
 5,6

      

I simply cannot remove this extra space in a "clean" way (ie: without post-processing the result).

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

+3


source to share


2 answers


I think the only thing missing was the outer-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;"
)

      

+3


source


For completeness, there is a second answer that doesn't require changing the request.

By default, extra spaces are added between elements during serialization.

The original query will output the desired output (without any extra whitespace) if you set the serialization parameter item-separator

to an empty string, which will bypass the default behavior. Each engine has its own API for this.

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



!item-separator=''

      

For Zorba:

-z item-separator=''

      

+1


source







All Articles