Re: Multiple different serialization formats in a single xquery
Pieter Lamers <[email protected]> Tue, 22 Apr 2025 07:15:21 +0200
| Newsgroups | gmane.text.xml.exist |
|---|---|
| Message-ID | <[email protected]> |
Hi Sascha,
I think the problem is that you can serialize a snippet using
fn:serialize but the whole response is serialized according to exist
serialization settings. Apart from the declare options route in the
prolog of your xquery, you can also set these inline via the following,
e.g. util:declare-option("exist:serialize", "method=json")
I think this will get you going.
Best,
Pieter
On 17/04/2025 15:25, Sascha Grabsch via Exist-open wrote:
> 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
> (<html><head><title>Test
> Title</title></head><body><h1>Hello,
> World!</h1></body></html>). 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 <!DOCTYPE html>
> <html><head><title>Test
> Title</title></head><body><h1>Hello,
> World!</h1></body></html>). 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
--
Pieter Lamers
John Benjamins Publishing Company
Postal Address: P.O. Box 36224, 1020 ME AMSTERDAM, The Netherlands
Visiting Address: Klaprozenweg 75D, 1033 NN AMSTERDAM, The Netherlands
Warehouse: Kelvinstraat 11-13, 1446 TK PURMEREND, The Netherlands
tel: +31 20 630 4747
web: www.benjamins.com
_______________________________________________
Exist-open mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/exist-open