[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

Michael Kay mike at saxonica.com
Fri Oct 17 16:04:47 PDT 2008


  Whitespace problem in CSV-like output
Your query is returning a sequence of strings, and when that gets serialized
using the text output method, the strings are shown space-separated. (You
would appreciate this if your query were simply "//property1/string()".)

The obvious way to prevent this is to wrap your whole query in

string-join(<your query here>, '')

In fact you could then get rid of the concat() from your query and use
&#x10; as the second argument of string-join() instead.

Doing that directly might not be very legible: you could put the query in a
function to get around this.

> (Another question: Within my "quantity" elements, I use the 
> German decimal separator which is a comma. Is there a more 
> elegant way than translating it to a dot and back, like 
> above, when summing up the values found in this element?)

Not really. You can do simple translations like this using translate()
rather than replace(), but it's no less code.

Michael Kay
http://www.saxonica.com/

 

> -----Original Message-----
> From: http://x-query.com/mailman/listinfo/talk 
> [mailto:http://x-query.com/mailman/listinfo/talk] On Behalf Of Yves Forkl
> Sent: 17 October 2008 14:12
> To: http://x-query.com/mailman/listinfo/talk
> Subject:  Whitespace problem in CSV-like output
> 
> Hi,
> 
> from a very simple XML input, I would like to create a 
> CSV-like output, putting each record (as I see them) in a 
> line of its own, with its fields separated by some delimiter 
> like a semicolon.
> 
> It works quite well, except that a space gets inserted at the 
> front of each line starting from the second line in the output.
> 
> My demo input is:
> 
> <?xml version='1.0'?>
> <list>
>    <entry>
>      <property1>A</property1>
>      <property2>33</property2>
>      <quantity>1,5</quantity>
>    </entry>
>    <entry>
>      <property1>A</property1>
>      <property2>33</property2>
>      <quantity>0,5</quantity>
>    </entry>
>    <entry>
>      <property1>B</property1>
>      <property2>22</property2>
>      <quantity>0,5</quantity>
>    </entry>
> </list>
> 
> My XQuery file has:
> 
> declare namespace saxon="http://saxon.sf.net/"; declare 
> option saxon:output "method=text";
> 
> 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(
>      concat(
>        string-join(
>          ($property1, $property2,
>           replace(xs:string(
>                     sum(for $quantity in $selected_entries/quantity
>                         return xs:decimal(replace($quantity, 
> ",", ".")))),
>                   "\.",
>                   ",")),
>          ";"),
>        "&#10;"))
> 
> The result as obtained with Saxon 8.8.0.4J is:
> 
> A;33;2
>   B;22;0,5
> 
> It is the space at the beginning of the second line that I 
> can't get away with. Can you explain what I am doing wrong 
> here and maybe suggest a solution?
> 
> (Another question: Within my "quantity" elements, I use the 
> German decimal separator which is a comma. Is there a more 
> elegant way than translating it to a dot and back, like 
> above, when summing up the values found in this element?)
> 
>    Yves
> _______________________________________________
> http://x-query.com/mailman/listinfo/talk
> http://x-query.com/mailman/listinfo/talk



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-2007 All Rights Reserved.