Output script-element with javascript source code
Tuesday, 7 December 2021 · 3 messages
KK
Kristian Kankainen7 Dec 2021 10:00 #
Hi all,
Can I somehow control the serialization of element contents? I want to use Xquery to generate parts of source code for javascript and put the generated code inside a script element on the webpage. Basically this:
define function web:display-graph($term) {
<script>
{
string-join((
'd3.select("#graph").graphviz().renderDot(',
"'digraph {a -> b}'",
');'
), out:nl())
}
</script>
}But it doesn't work as the > character gets replaced with its corresponding HTML entity >. Is there a way to circumvent this behaviour?
Best regards
Kristian Kankainen
MH
Martin Honnen7 Dec 2021 10:27 #
Quoted text · Kristian Kankainen · 19 lines
Am 07.12.2021 um 10:00 schrieb Kristian Kankainen: > Hi all, > > Can I somehow control the serialization of element contents? I want to use Xquery to generate parts of source code for javascript and put the generated code inside a script element on the webpage. Basically this: > > define function web:display-graph($term) { > <script> > { > string-join(( > 'd3.select("#graph").graphviz().renderDot(', > "'digraph {a -> b}'", > ');' > ), out:nl()) > } > </script> > } > > But it doesn't work as the > character gets replaced with its corresponding HTML entity >. Is there a way to circumvent this behaviour? >
At which point or how/where do you serialize that "script" element? With output method "html" the escaping should not take place.
KK
Kristian Kankainen7 Dec 2021 10:40 #
Thank you Martin! I used output method "xhtml". After changing it to "html" the string was not escaped any longer. Great!
Best regards,
Kristian Kankainen
Quoted text · Martin Honnen · 25 lines
> On 7. Dec 2021, at 11:27, Martin Honnen <martin.honnen at gmx.de> wrote: > > > Am 07.12.2021 um 10:00 schrieb Kristian Kankainen: >> Hi all, >> >> Can I somehow control the serialization of element contents? I want to use Xquery to generate parts of source code for javascript and put the generated code inside a script element on the webpage. Basically this: >> >> define function web:display-graph($term) { >> <script> >> { >> string-join(( >> 'd3.select("#graph").graphviz().renderDot(', >> "'digraph {a -> b}'", >> ');' >> ), out:nl()) >> } >> </script> >> } >> >> But it doesn't work as the > character gets replaced with its corresponding HTML entity >. Is there a way to circumvent this behaviour? >> > > At which point or how/where do you serialize that "script" element? With > output method "html" the escaping should not take place.
Written for BaseX 9.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery