[xquery-talk] Whitespace problem in CSV-like output

John Snelson john.snelson at oracle.com
Fri Oct 17 16:18:19 PDT 2008


Hi Yves,

The whitespace is added as a separator when the multiple strings 
returned from your query are concatenated. To stop it, wrap the entire 
query in a call to string-join(), like this:

string-join(
let $entries := /list/entry
for $property1 in distinct-values($entries/property1),

...<snip>...

         ";"),
       "&#10;")),
"")

XQuery doesn't have a built in way to read numbers using decimal commas, 
so the way you are doing it is about as good as it gets.

John

Yves Forkl wrote:
> 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
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk


-- 
John Snelson, Oracle Corporation            http://snelson.org.uk/john
Berkeley DB XML:            http://oracle.com/database/berkeley-db/xml
XQilla:                                  http://xqilla.sourceforge.net


More information about the talk mailing list