Search code examples
xmlformattingxqueryxqilla

Xquery result not correctly formatted


I've just started using Xquery with Xqilla.

I want my result to be returned with correct indentation and newlines.

I have a list of directors called $duplicates, and I want to fetch all movie titles from these directors within my return statement.

My return statement:

for $duplicate in $duplicates
return 
    <movie director="{$duplicate}"> 
    {$result/videos/video/title[../director=$duplicate]}</movie>

Which I want to be formatted (kind of) like this:

<movie director="Coppola">
    <title>The Godfather</title>
    <title>The Godfather pt. 2</title>
    <title>Apocalypse Now</title></movie>

with the children properly indented, each on a new line.

What I'm getting is this:

<movie director="Coppola">
     <title>The Godfather</title><title>The Godfather pt. 2</title><title>Apocalypse Now</title></movie>

with all of the titles in a single row.

Since I've successfully returned a list of titles before by simply returning a variable I tried a nested return statement with a for-loop as suggested on wikibooks. This solution also contained explicitly defined <title>-tags.

I found one person with the same problem here on SO.

The answer there did not work. When I added a namespace my program stopped outputting completely. I'd also prefer it if there was some non-kludge solution (which there seems to be since everyone seems to be getting perfect output right from the start).

The problem seems to be about mixing XML and Xquery with curly bracket usage.


Solution

  • This isn't a matter of a problem with your query, as much as it a matter of the flags and options set to control output serialization.

    What you probably want is to set the XQilla format-pretty-print flag to true, or (for XQuery 3.0) use the indent XQuery serialization option in your prolog, like so:

    declare option output:indent "yes";
    

    If this is for command-line use and you don't have XQuery 3.0 support yet available, you can also punt by using a different pretty-printing tool (if there isn't a clear way to set serialization options in XQilla). For instance, piping output to xmlstarlet fo will pretty-print it in a second pass.