eXist-db / exist

eXist Native XML Database and Application Platform
https://exist-db.org
GNU Lesser General Public License v2.1
429 stars 179 forks source link

[BUG] serialization parameter cdata-section-elements not fully implemented #5133

Open PieterLamers opened 11 months ago

PieterLamers commented 11 months ago

Describe the bug a CDATA section is not added to the output for an element x when there is a serialization option as follows: declare option output:cdata-section-elements "x";

Expected behavior I expect the output to contain a CDATA wrapper for the elements listed in the cdata-section-elements list, wrapping the content of the element. Quoting Priscilla's book: "cdata-section-elements A space-separated list of qualified element names whose contents should be enclosed in a CDATA section in XML output. For example, if your Docbook output contains some escaped XML examples, the output might be written as: <programlisting>&lt;p>Example p content&lt;/p></programlisting> with escaped less-than characters. If you list the programlisting element in the cdata-section-elements parameter, the output will be more readable: <programlisting><![CDATA[<p>Example p content</p>]]></programlisting> The output XML document is exactly equivalent; CDATA sections are merely used for convenience when editing documents."

To Reproduce The following XQuery shows my current use case: outputting a constructed URL without escaped the &. I have played with various serialization settings but did not find any that would produce the CDATA.

xquery version "3.1";

declare namespace output = 'http://www.w3.org/2010/xslt-xquery-serialization';

declare option output:method "xml";
declare option output:media-type "application/xml";
declare option output:indent 'no';

declare option output:cdata-section-elements "script";

let $script as element(script) := <script>localhost:8080/exist/apps/myapp?my1=1&amp;my2=2</script> 

let $html :=
  <html>
  <head>
    <title>HTML ampersand test</title>
    {$script}
  </head>
  <body>
    <h1>TEST</h1>
  </body>
  </html>

return $html

This code will produce the CDATA section when run from Saxon XQuery (EE 12.3, run from Oxygen): <?xml version="1.0" encoding="UTF-8"?><html><head><title>HTML ampersand test</title><script><![CDATA[localhost:8080/exist/apps/myapp?my1=1&my2=2]]></script></head><body><h1>TEST</h1></body></html>

but not when run from eXist-db 6.2.0.: <html><head><title>HTML ampersand test</title><script>localhost:8080/exist/apps/myapp?my1=1&amp;my2=2</script></head><body><h1>TEST</h1></body></html>

It seems to me that the case of adding a CDATA wrapper, as mentioned in the XQuery book, was not addressed with https://github.com/eXist-db/exist/issues/2233 but merely the preservation of previously created CDATA sections. Pinging @adamretter and @joewiz for comments.

PieterLamers commented 11 months ago

And here is the link from the spec, also cited in #2233: https://www.w3.org/TR/xslt-xquery-serialization-31/#XML_CDATA-SECTION-ELEMENTS