| 1 | <!doctype html> |
| 2 | |
| 3 | <title>CodeMirror: XQuery mode</title> |
| 4 | <meta charset="utf-8"/> |
| 5 | <link rel=stylesheet href="../../doc/docs.css"> |
| 6 | |
| 7 | <link rel="stylesheet" href="../../lib/codemirror.css"> |
| 8 | <link rel="stylesheet" href="../../theme/xq-dark.css"> |
| 9 | <script src="../../lib/codemirror.js"></script> |
| 10 | <script src="../../addon/edit/matchbrackets.js"></script> |
| 11 | <script src="xquery.js"></script> |
| 12 | <style> |
| 13 | .CodeMirror { |
| 14 | border-top: 1px solid black; border-bottom: 1px solid black; |
| 15 | height:400px; |
| 16 | } |
| 17 | </style> |
| 18 | <div id=nav> |
| 19 | <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a> |
| 20 | |
| 21 | <ul> |
| 22 | <li><a href="../../index.html">Home</a> |
| 23 | <li><a href="../../doc/manual.html">Manual</a> |
| 24 | <li><a href="https://github.com/codemirror/codemirror">Code</a> |
| 25 | </ul> |
| 26 | <ul> |
| 27 | <li><a href="../index.html">Language modes</a> |
| 28 | <li><a class=active href="#">XQuery</a> |
| 29 | </ul> |
| 30 | </div> |
| 31 | |
| 32 | <article> |
| 33 | <h2>XQuery mode</h2> |
| 34 | |
| 35 | |
| 36 | <div class="cm-s-default"> |
| 37 | <textarea id="code" name="code"> |
| 38 | xquery version "1.0-ml"; |
| 39 | (: this is |
| 40 | : a |
| 41 | "comment" :) |
| 42 | let $let := <x attr="value">"test"<func>function() $var {function()} {$var}</func></x> |
| 43 | let $joe:=1 |
| 44 | return element element { |
| 45 | attribute attribute { 1 }, |
| 46 | element test { 'a' }, |
| 47 | attribute foo { "bar" }, |
| 48 | fn:doc()[ foo/@bar eq $let ], |
| 49 | //x } |
| 50 | |
| 51 | (: a more 'evil' test :) |
| 52 | (: Modified Blakeley example (: with nested comment :) ... :) |
| 53 | declare private function local:declare() {()}; |
| 54 | declare private function local:private() {()}; |
| 55 | declare private function local:function() {()}; |
| 56 | declare private function local:local() {()}; |
| 57 | let $let := <let>let $let := "let"</let> |
| 58 | return element element { |
| 59 | attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } }, |
| 60 | attribute fn:doc { "bar" castable as xs:string }, |
| 61 | element text { text { "text" } }, |
| 62 | fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ], |
| 63 | //fn:doc |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | xquery version "1.0-ml"; |
| 69 | |
| 70 | (: Copyright 2006-2010 Mark Logic Corporation. :) |
| 71 | |
| 72 | (: |
| 73 | : Licensed under the Apache License, Version 2.0 (the "License"); |
| 74 | : you may not use this file except in compliance with the License. |
| 75 | : You may obtain a copy of the License at |
| 76 | : |
| 77 | : http://www.apache.org/licenses/LICENSE-2.0 |
| 78 | : |
| 79 | : Unless required by applicable law or agreed to in writing, software |
| 80 | : distributed under the License is distributed on an "AS IS" BASIS, |
| 81 | : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 82 | : See the License for the specific language governing permissions and |
| 83 | : limitations under the License. |
| 84 | :) |
| 85 | |
| 86 | module namespace json = "http://marklogic.com/json"; |
| 87 | declare default function namespace "http://www.w3.org/2005/xpath-functions"; |
| 88 | |
| 89 | (: Need to backslash escape any double quotes, backslashes, and newlines :) |
| 90 | declare function json:escape($s as xs:string) as xs:string { |
| 91 | let $s := replace($s, "\\", "\\\\") |
| 92 | let $s := replace($s, """", "\\""") |
| 93 | let $s := replace($s, codepoints-to-string((13, 10)), "\\n") |
| 94 | let $s := replace($s, codepoints-to-string(13), "\\n") |
| 95 | let $s := replace($s, codepoints-to-string(10), "\\n") |
| 96 | return $s |
| 97 | }; |
| 98 | |
| 99 | declare function json:atomize($x as element()) as xs:string { |
| 100 | if (count($x/node()) = 0) then 'null' |
| 101 | else if ($x/@type = "number") then |
| 102 | let $castable := $x castable as xs:float or |
| 103 | $x castable as xs:double or |
| 104 | $x castable as xs:decimal |
| 105 | return |
| 106 | if ($castable) then xs:string($x) |
| 107 | else error(concat("Not a number: ", xdmp:describe($x))) |
| 108 | else if ($x/@type = "boolean") then |
| 109 | let $castable := $x castable as xs:boolean |
| 110 | return |
| 111 | if ($castable) then xs:string(xs:boolean($x)) |
| 112 | else error(concat("Not a boolean: ", xdmp:describe($x))) |
| 113 | else concat('"', json:escape($x), '"') |
| 114 | }; |
| 115 | |
| 116 | (: Print the thing that comes after the colon :) |
| 117 | declare function json:print-value($x as element()) as xs:string { |
| 118 | if (count($x/*) = 0) then |
| 119 | json:atomize($x) |
| 120 | else if ($x/@quote = "true") then |
| 121 | concat('"', json:escape(xdmp:quote($x/node())), '"') |
| 122 | else |
| 123 | string-join(('{', |
| 124 | string-join(for $i in $x/* return json:print-name-value($i), ","), |
| 125 | '}'), "") |
| 126 | }; |
| 127 | |
| 128 | (: Print the name and value both :) |
| 129 | declare function json:print-name-value($x as element()) as xs:string? { |
| 130 | let $name := name($x) |
| 131 | let $first-in-array := |
| 132 | count($x/preceding-sibling::*[name(.) = $name]) = 0 and |
| 133 | (count($x/following-sibling::*[name(.) = $name]) > 0 or $x/@array = "true") |
| 134 | let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) > 0 |
| 135 | return |
| 136 | |
| 137 | if ($later-in-array) then |
| 138 | () (: I was handled previously :) |
| 139 | else if ($first-in-array) then |
| 140 | string-join(('"', json:escape($name), '":[', |
| 141 | string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), ","), |
| 142 | ']'), "") |
| 143 | else |
| 144 | string-join(('"', json:escape($name), '":', json:print-value($x)), "") |
| 145 | }; |
| 146 | |
| 147 | (:~ |
| 148 | Transforms an XML element into a JSON string representation. See http://json.org. |
| 149 | <p/> |
| 150 | Sample usage: |
| 151 | <pre> |
| 152 | xquery version "1.0-ml"; |
| 153 | import module namespace json="http://marklogic.com/json" at "json.xqy"; |
| 154 | json:serialize(&lt;foo&gt;&lt;bar&gt;kid&lt;/bar&gt;&lt;/foo&gt;) |
| 155 | </pre> |
| 156 | Sample transformations: |
| 157 | <pre> |
| 158 | &lt;e/&gt; becomes {"e":null} |
| 159 | &lt;e&gt;text&lt;/e&gt; becomes {"e":"text"} |
| 160 | &lt;e&gt;quote " escaping&lt;/e&gt; becomes {"e":"quote \" escaping"} |
| 161 | &lt;e&gt;backslash \ escaping&lt;/e&gt; becomes {"e":"backslash \\ escaping"} |
| 162 | &lt;e&gt;&lt;a&gt;text1&lt;/a&gt;&lt;b&gt;text2&lt;/b&gt;&lt;/e&gt; becomes {"e":{"a":"text1","b":"text2"}} |
| 163 | &lt;e&gt;&lt;a&gt;text1&lt;/a&gt;&lt;a&gt;text2&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1","text2"]}} |
| 164 | &lt;e&gt;&lt;a array="true"&gt;text1&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1"]}} |
| 165 | &lt;e&gt;&lt;a type="boolean"&gt;false&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":false}} |
| 166 | &lt;e&gt;&lt;a type="number"&gt;123.5&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":123.5}} |
| 167 | &lt;e quote="true"&gt;&lt;div attrib="value"/&gt;&lt;/e&gt; becomes {"e":"&lt;div attrib=\"value\"/&gt;"} |
| 168 | </pre> |
| 169 | <p/> |
| 170 | Namespace URIs are ignored. Namespace prefixes are included in the JSON name. |
| 171 | <p/> |
| 172 | Attributes are ignored, except for the special attribute @array="true" that |
| 173 | indicates the JSON serialization should write the node, even if single, as an |
| 174 | array, and the attribute @type that can be set to "boolean" or "number" to |
| 175 | dictate the value should be written as that type (unquoted). There's also |
| 176 | an @quote attribute that when set to true writes the inner content as text |
| 177 | rather than as structured JSON, useful for sending some XHTML over the |
| 178 | wire. |
| 179 | <p/> |
| 180 | Text nodes within mixed content are ignored. |
| 181 | |
| 182 | @param $x Element node to convert |
| 183 | @return String holding JSON serialized representation of $x |
| 184 | |
| 185 | @author Jason Hunter |
| 186 | @version 1.0.1 |
| 187 | |
| 188 | Ported to xquery 1.0-ml; double escaped backslashes in json:escape |
| 189 | :) |
| 190 | declare function json:serialize($x as element()) as xs:string { |
| 191 | string-join(('{', json:print-name-value($x), '}'), "") |
| 192 | }; |
| 193 | </textarea> |
| 194 | </div> |
| 195 | |
| 196 | <script> |
| 197 | var editor = CodeMirror.fromTextArea(document.getElementById("code"), { |
| 198 | lineNumbers: true, |
| 199 | matchBrackets: true, |
| 200 | theme: "xq-dark" |
| 201 | }); |
| 202 | </script> |
| 203 | |
| 204 | <p><strong>MIME types defined:</strong> <code>application/xquery</code>.</p> |
| 205 | |
| 206 | <p>Development of the CodeMirror XQuery mode was sponsored by |
| 207 | <a href="http://marklogic.com">MarkLogic</a> and developed by |
| 208 | <a href="https://twitter.com/mbrevoort">Mike Brevoort</a>. |
| 209 | </p> |
| 210 | |
| 211 | </article> |