Write expression inside xml comment

Viewed 90

I'd like to put xml comment in the output and put some expression inside the comment. How can I do that in xquery? If I have

<!-- {$var} -->

it is inserted literally, but I'd like to have a comment tag on the output with the value of $var

2 Answers

XQuery has a comment constructor, for example:

<test>
{
  let $x := 'hello world!'
  return comment {$x}
}
</test>

yields:

<?xml version="1.0" encoding="UTF-8"?>
<test><!--hello world!--></test>

Would something like this work?

for $x in /* return <x><![CDATA[<!--]]>{$x}<![CDATA[-->]]> </x>

input

<test>hello comment</test>

output

<!--hello comment--> 
Related