BenSimonds
BenSimonds

Reputation: 103

Escaping greater than symbols in javascript within xquery

I'm trying to embed a short bit of javascript inside my xquery, and I cant seem to get the greater than symbols to behave. I've escaped them, but they appear as > in my javascript code when I view the results.

Here's my code:

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

xdmp:set-response-content-type("text/html; charset=utf-8"),
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> &gt; This works...
<script>
var five = 5;
var is_more = function(n) {{
    if n &gt; 2 {{
        return "Yes";
    }} else {{
        return "no";
    }};
}}
console.log(is_more(five));
</script>
</html>

But whilst escaping works for text, it doesn't work within my javascript: Not escaping greater than correctly.

I'm sure it's something obvious but I'm pretty new to xquery.

Upvotes: 0

Views: 440

Answers (2)

Jens Erat
Jens Erat

Reputation: 38712

Simply put the actual script into commented out CDATA tags (//<![CDATA[ ... //]]>) or XML comments (//<!-- ... -->), which will prevent <, > and & characters to be encoded as XML entities.

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

xdmp:set-response-content-type("text/html; charset=utf-8"),
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> &gt; This works...
<script>
//<![CDATA[
var five = 5;
var is_more = function(n) {{
    if (n > 2) {{
        return "Yes";
    }} else {{
        return "no";
    }};
}}
console.log(is_more(five));
//]]>
</script>
</html>

CDATA tags would be the more elegant way, but depend on the serialization options set (some engines might remove them). Comments are probably more fail-save when not explicitly setting serialization options, which might prove difficult in XQuery 1.0, as it was not standardized before XQuery 3.0. Defining the document as XHTML should also trigger the browser to correctly parse the entities and use the decoded characters for JavaScript instead.

Upvotes: 0

dan
dan

Reputation: 61

Put the script in a comment block. You also need parentheses in the if statement.

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

xdmp:set-response-content-type("text/html; charset=utf-8"),
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> &gt; This works...
    <script>
        <!--
        var five = 5;
        var is_more = function(n) {
            if (n > 2) {
                return "Yes";
            } else {
                return "no";
            };
        }
        console.log(is_more(five));
        -->
    </script>
</html>

Upvotes: 2

Related Questions