Search code examples
xquery

Write expression inside xml comment


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


Solution

  • 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>