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

Michael Kay mike at saxonica.com
Mon Oct 20 12:35:51 PDT 2008

> > A trick is to use text nodes, because that suppresses the 
> extra spaces:
> This provides for a really neat solution, thank you very much 
> for this suggestion.
> Trying to understand why it is working (it does, tested it 
> with Saxon): 

It's basically a side-effect of the rules for constructing element content.

If you do this:

<a>{for $i in 1 to 10 return $i*$i}</a>

you get

<a>1 4 9 16 25 36 49 64 81 100</a>

But if you do this:

let $input := <p>A <i>large</i> flock of <b>geese</b> flew overhead</p>
return <a>{$input//text()}</a>

you get

<a>A large flock of geese flew overhead</a>

To achieve this, the rule is that a space is inserted between two atomic
values, but it is not inserted between two text nodes, or between an atomic
value and a text node. Per's solution exploits this by creating a text node
as a way to disable the insertion of spaces. (I considered the same solution
myself, but thought it too obscure.)

The rules might seem strange, but the idea is that they give the right
answer most of the time without you having to think about it. You've hit on
an example where that isn't the case, unfortunately.

Michael Kay

More information about the talk mailing list