5 Copyright (C) 2011 by MarkLogic Corporation
6 Author: Mike Brevoort <mike@brevoort.com>
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 <meta charset="utf-8">
29 <title>CodeMirror: XQuery mode</title>
30 <link rel="stylesheet" href="../../lib/codemirror.css">
31 <script src="../../lib/codemirror.js"></script>
32 <script src="xquery.js"></script>
33 <link rel="stylesheet" href="../../doc/docs.css">
34 <link rel="stylesheet" href="../../theme/xq-dark.css">
35 <style type="text/css">
37 border-top: 1px solid black; border-bottom: 1px solid black;
43 <h1>CodeMirror: XQuery mode</h1>
45 <div class="cm-s-default">
46 <textarea id="code" name="code">
47 xquery version "1.0-ml";
51 let $let := <x attr="value">"test"<func>function() $var {function()} {$var}</func></x>
53 return element element {
54 attribute attribute { 1 },
55 element test { 'a' },
56 attribute foo { "bar" },
57 fn:doc()[ foo/@bar eq $let ],
60 (: a more 'evil' test :)
61 (: Modified Blakeley example (: with nested comment :) ... :)
62 declare private function local:declare() {()};
63 declare private function local:private() {()};
64 declare private function local:function() {()};
65 declare private function local:local() {()};
66 let $let := <let>let $let := "let"</let>
67 return element element {
68 attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } },
69 attribute fn:doc { "bar" castable as xs:string },
70 element text { text { "text" } },
71 fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ],
77 xquery version "1.0-ml";
79 (: Copyright 2006-2010 Mark Logic Corporation. :)
82 : Licensed under the Apache License, Version 2.0 (the "License");
83 : you may not use this file except in compliance with the License.
84 : You may obtain a copy of the License at
86 : http://www.apache.org/licenses/LICENSE-2.0
88 : Unless required by applicable law or agreed to in writing, software
89 : distributed under the License is distributed on an "AS IS" BASIS,
90 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
91 : See the License for the specific language governing permissions and
92 : limitations under the License.
95 module namespace json = "http://marklogic.com/json";
96 declare default function namespace "http://www.w3.org/2005/xpath-functions";
98 (: Need to backslash escape any double quotes, backslashes, and newlines :)
99 declare function json:escape($s as xs:string) as xs:string {
100 let $s := replace($s, "\\", "\\\\")
101 let $s := replace($s, """", "\\""")
102 let $s := replace($s, codepoints-to-string((13, 10)), "\\n")
103 let $s := replace($s, codepoints-to-string(13), "\\n")
104 let $s := replace($s, codepoints-to-string(10), "\\n")
108 declare function json:atomize($x as element()) as xs:string {
109 if (count($x/node()) = 0) then 'null'
110 else if ($x/@type = "number") then
111 let $castable := $x castable as xs:float or
112 $x castable as xs:double or
113 $x castable as xs:decimal
115 if ($castable) then xs:string($x)
116 else error(concat("Not a number: ", xdmp:describe($x)))
117 else if ($x/@type = "boolean") then
118 let $castable := $x castable as xs:boolean
120 if ($castable) then xs:string(xs:boolean($x))
121 else error(concat("Not a boolean: ", xdmp:describe($x)))
122 else concat('"', json:escape($x), '"')
125 (: Print the thing that comes after the colon :)
126 declare function json:print-value($x as element()) as xs:string {
127 if (count($x/*) = 0) then
129 else if ($x/@quote = "true") then
130 concat('"', json:escape(xdmp:quote($x/node())), '"')
133 string-join(for $i in $x/* return json:print-name-value($i), ","),
137 (: Print the name and value both :)
138 declare function json:print-name-value($x as element()) as xs:string? {
139 let $name := name($x)
140 let $first-in-array :=
141 count($x/preceding-sibling::*[name(.) = $name]) = 0 and
142 (count($x/following-sibling::*[name(.) = $name]) > 0 or $x/@array = "true")
143 let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) > 0
146 if ($later-in-array) then
147 () (: I was handled previously :)
148 else if ($first-in-array) then
149 string-join(('"', json:escape($name), '":[',
150 string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), ","),
153 string-join(('"', json:escape($name), '":', json:print-value($x)), "")
157 Transforms an XML element into a JSON string representation. See http://json.org.
161 xquery version "1.0-ml";
162 import module namespace json="http://marklogic.com/json" at "json.xqy";
163 json:serialize(&lt;foo&gt;&lt;bar&gt;kid&lt;/bar&gt;&lt;/foo&gt;)
165 Sample transformations:
167 &lt;e/&gt; becomes {"e":null}
168 &lt;e&gt;text&lt;/e&gt; becomes {"e":"text"}
169 &lt;e&gt;quote " escaping&lt;/e&gt; becomes {"e":"quote \" escaping"}
170 &lt;e&gt;backslash \ escaping&lt;/e&gt; becomes {"e":"backslash \\ escaping"}
171 &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"}}
172 &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"]}}
173 &lt;e&gt;&lt;a array="true"&gt;text1&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1"]}}
174 &lt;e&gt;&lt;a type="boolean"&gt;false&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":false}}
175 &lt;e&gt;&lt;a type="number"&gt;123.5&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":123.5}}
176 &lt;e quote="true"&gt;&lt;div attrib="value"/&gt;&lt;/e&gt; becomes {"e":"&lt;div attrib=\"value\"/&gt;"}
179 Namespace URIs are ignored. Namespace prefixes are included in the JSON name.
181 Attributes are ignored, except for the special attribute @array="true" that
182 indicates the JSON serialization should write the node, even if single, as an
183 array, and the attribute @type that can be set to "boolean" or "number" to
184 dictate the value should be written as that type (unquoted). There's also
185 an @quote attribute that when set to true writes the inner content as text
186 rather than as structured JSON, useful for sending some XHTML over the
189 Text nodes within mixed content are ignored.
191 @param $x Element node to convert
192 @return String holding JSON serialized representation of $x
197 Ported to xquery 1.0-ml; double escaped backslashes in json:escape
199 declare function json:serialize($x as element()) as xs:string {
200 string-join(('{', json:print-name-value($x), '}'), "")
206 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
213 <p><strong>MIME types defined:</strong> <code>application/xquery</code>.</p>
215 <p>Development of the CodeMirror XQuery mode was sponsored by
216 <a href="http://marklogic.com">MarkLogic</a> and developed by
217 <a href="https://twitter.com/mbrevoort">Mike Brevoort</a>.