[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

Whitespace problem in CSV-like output

Yves Forkl Y.Forkl at srz.de
Fri Oct 17 18:19:38 PDT 2008


  Whitespace problem in CSV-like output
I am very grateful to both of you for your valuable suggestions.

It is a bit disappointing to see that XQuery does not offer a simple, 
general method to create CSV-type files, at least not in my case.

I have picked up the idea to use functions, but rather than 
encapsulating the query, I (for now) prefer to put the string 
manipulation operations into functions so that I can reuse them and use 
them for parametrization.

So now I have:

xquery version "1.0" encoding "ISO-8859-1";
declare namespace saxon="http://saxon.sf.net/";
declare namespace my="http://xmlns.srz.de/yforkl/xquery/functions";

declare option saxon:output "method=text";

declare function my:join-fields-into-record
   ( $fields as xs:string*,
     $delimiter as xs:string ) as xs:string {
       concat(string-join($fields, $delimiter), "
")
  } ;
declare function my:join-records-into-lines
   ( $records as xs:string* ) as xs:string {
       string-join($records, "")
  } ;

my:join-records-into-lines(
   let $entries := /list/entry

   for $property1 in distinct-values($entries/property1),
       $property2 in distinct-values(
         $entries[property1 = $property1]/property2)
     let $selected_entries := $entries[property1 = $property1 and
                                       property2 = $property2]
     order by $property1, $property2
     return(
       my:join-fields-into-record(
         ($property1,
          $property2,
          translate(
            xs:string(
              sum(
                for $quantity in $selected_entries/quantity
                     return xs:decimal(translate($quantity,
                                                 ",",
                                                 ".")))),
                    ".",
                    ",")),
         ";"))
)

NB: While the names of the functions seem to suggest that the line feed 
should rather be appended in my:join-records-into-lines than in 
my:join-fields-into-record, I don't see any easy way to do this.

   Yves


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.