Re: Multiple different serialization formats in a single xquery

Sascha Grabsch via Exist-open <[email protected]> Wed, 23 Apr 2025 13:26:52 +0200
Newsgroups gmane.text.xml.exist
Message-ID <[email protected]>
Dear Pieter, dear Peter,

thanks a lot for your suggestions! I tried out Pieters 
util:declare-options() solution and it seems to work fine so far. For 
documentations sake, here is the updated/fixed version of my attempt:

xquery version "3.1";
let $p_format := request:get-parameter("format", ())

let $test :=
<html>
     <head>
         <title>Test Title</title>
     </head>
     <body>
         <h1>Hello, World!</h1>
     </body>
</html>

return
     if ($p_format = "tei-xml") then 
(util:declare-option("exist:serialize", "method=xml"), $test) else 
(util:declare-option("exist:serialize", "method=html5 
media-type=text/html"), $test)


Serialization, doctype and Content-Type header look good with this variant.

Thanks a lot!

Sascha

Am 17.04.2025 um 15:25 schrieb Sascha Grabsch via Exist-open:
> Dear list members,
> 
> I'm trying to output different formats (XML and HTML5) from a single 
> xquery script, but can't get it to work. Here is my test case:
> 
> xquery version "3.1";
> let $p_format := request:get-parameter("format", ())
> 
> let $test :=
> <html>
>      <head>
>          <title>Test Title</title>
>      </head>
>      <body>
>          <h1>Hello, World!</h1>
>      </body>
> </html>
> 
> return
>      if ($p_format = "tei-xml") then (serialize($test, map { "method": 
> "xml" })) else (serialize($test, map { "method": "html" }))
> 
> 
> However in both cases this does not work as expected:
> 
> method:xml - The serialization option method:xml does not return valid 
> XML, since the returned document is wrongly escaped 
> (&lt;html&gt;&lt;head&gt;&lt;title&gt;Test Title&lt;/title&gt;&lt;/ 
> head&gt;&lt;body&gt;&lt;h1&gt;Hello, World!&lt;/h1&gt;&lt;/body&gt;&lt;/ 
> html&gt;). The content-type in the response header however is correct 
> ("application/xml").
> 
> method:html - While the serialization option method:html correctly adds 
> a doctype for HTML5 (i. e. <!DOCTYPE html>), the actual response in a 
> browser is wrongly escaped (as in &lt;!DOCTYPE html&gt;
> &lt;html&gt;&lt;head&gt;&lt;title&gt;Test Title&lt;/title&gt;&lt;/ 
> head&gt;&lt;body&gt;&lt;h1&gt;Hello, World!&lt;/h1&gt;&lt;/body&gt;&lt;/ 
> html&gt;). Additionally the content-type in the response header is 
> "application/xml" instead of "text/html".
> 
> 
> According to <https://exist-db.org/exist/apps/doc/xquery.xml#fn- 
> serialize> I would have expected to be able to return different 
> serializations via fn:serialize, but it seems it is only possible to 
> have one serialization per XQuery script via "declare option 
> exist:serialize ..."?
> 
> I would be glad about any information or hints what I might be missing.
> 
> Thanks a lot!
> 
> Sascha


_______________________________________________
Exist-open mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/exist-open