SteveCallender
SteveCallender

Reputation: 211

Putting Line Breaks in XQuery XML Source but NOT in Output

For a piece of code im writing i require the output of the XQuery to be on a single line. However, the XML markup is incredibly verbose. This is not an issue for the output of the program because it does not require to be human readable, but the source code (for maintenance purposes) of course does need to be human readable. Is there a nice clean way to put line breaks in the XQuery XML that do not flow through to the output? I currently am using the work around below.

You can pretty much ignore the code but note that this will all output on to a single line when processed. Notice the constant 'nl' being inserted whenever I want a line break in my source code.

let $nl := '';


 <type><name>void</name></type> <name>{fn:string($className)}__{fn:string($requestReceived/@name)}__received</name>{$nl
 }<parameter_list>(<param><decl><type><specifier>const</specifier> <name>{fn:replace($requestReceived/mt:input/@qtype,":","::")}</name> &amp;{$nl
 }</type><name>{fn:string($requestReceived/mt:input/@name)}</name></decl></param>,<param><decl><type><specifier>const</specifier> <name>{$nl
 }{fn:replace($requestReceived/mt:input/@qtype,":","::")}</name> &amp;</type><name>{fn:string($requestReceived/mt:output/@name)}</name></decl></param>)</parameter_list>;

Does anyone have a better alternative?

Upvotes: 0

Views: 625

Answers (1)

Jens Erat
Jens Erat

Reputation: 38662

Zorba supports XQuery 3.0 and the serialization options. The following should also be valid for all other XQuery 3.0 engines, I tested with both Zorba and BaseX.

Disable Indentation

Setting output:indent "no" will disable newlines for each result.

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:indent "no";

<a><b/></a>

Result:

<a><b/></a>

Disable Lines Breaks in-Between XML Snippets

If you don't return a single XML fragment, but a sequence of those, also set output:item-separator "" which will remove newlines in-between:

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:indent "no";
declare option output:item-separator "";

(<a><b/></a>, <foo/>)

Result:

<a><b/></a><foo/>

Upvotes: 1

Related Questions