| 776 |
lars |
1 |
<!doctype html>
|
|
|
2 |
<title>CodeMirror: XQuery mode</title>
|
|
|
3 |
<meta charset="utf-8" />
|
|
|
4 |
<link rel=stylesheet href="../../doc/docs.css">
|
|
|
5 |
<link rel="stylesheet" href="../../lib/codemirror.css">
|
|
|
6 |
<link rel="stylesheet" href="../../theme/xq-dark.css">
|
|
|
7 |
<script src="../../lib/codemirror.js"></script>
|
|
|
8 |
<script src="xquery.js"></script>
|
|
|
9 |
<style type="text/css">
|
|
|
10 |
.CodeMirror {
|
|
|
11 |
border-top: 1px solid black;
|
|
|
12 |
border-bottom: 1px solid black;
|
|
|
13 |
height: 400px;
|
|
|
14 |
}
|
|
|
15 |
</style>
|
|
|
16 |
<div id=nav>
|
|
|
17 |
<a href="http://codemirror.net">
|
|
|
18 |
<h1>CodeMirror</h1>
|
|
|
19 |
<img id=logo src="../../doc/logo.png">
|
|
|
20 |
</a>
|
|
|
21 |
<ul>
|
|
|
22 |
<li>
|
|
|
23 |
<a href="../../index.html">Home</a>
|
|
|
24 |
<li>
|
|
|
25 |
<a href="../../doc/manual.html">Manual</a>
|
|
|
26 |
<li>
|
|
|
27 |
<a href="https://github.com/codemirror/codemirror">Code</a>
|
|
|
28 |
</ul>
|
|
|
29 |
<ul>
|
|
|
30 |
<li>
|
|
|
31 |
<a href="../index.html">Language modes</a>
|
|
|
32 |
<li>
|
|
|
33 |
<a class=active href="#">XQuery</a>
|
|
|
34 |
</ul>
|
|
|
35 |
</div>
|
|
|
36 |
<article>
|
|
|
37 |
<h2>XQuery mode</h2>
|
|
|
38 |
<div class="cm-s-default">
|
|
|
39 |
<textarea id="code" name="code"> xquery version "1.0-ml"; (: this is : a "comment" :) let $let := <x attr="value">"test"<func>function() $var {function()} {$var}</func></x> let $joe:=1 return element element { attribute attribute
|
|
|
40 |
{ 1 }, element test { 'a' }, attribute foo { "bar" }, fn:doc()[ foo/@bar eq $let ], //x } (: a more 'evil' test :) (: Modified Blakeley example (: with nested comment :) ... :) declare private function local:declare() {()};
|
|
|
41 |
declare private function local:private() {()}; declare private function local:function() {()}; declare private function local:local() {()}; let $let := <let>let $let := "let"</let> return element element { attribute attribute
|
|
|
42 |
{ try { xdmp:version() } catch($e) { xdmp:log($e) } }, attribute fn:doc { "bar" castable as xs:string }, element text { text { "text" } }, fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ], //fn:doc } xquery version
|
|
|
43 |
"1.0-ml"; (: Copyright 2006-2010 Mark Logic Corporation. :) (: : Licensed under the Apache License, Version 2.0 (the "License"); : you may not use this file except in compliance with the License. : You may obtain a copy
|
|
|
44 |
of the License at : : http://www.apache.org/licenses/LICENSE-2.0 : : Unless required by applicable law or agreed to in writing, software : distributed under the License is distributed on an "AS IS" BASIS, : WITHOUT WARRANTIES OR
|
|
|
45 |
CONDITIONS OF ANY KIND, either express or implied. : See the License for the specific language governing permissions and : limitations under the License. :) module namespace json = "http://marklogic.com/json"; declare default function
|
|
|
46 |
namespace "http://www.w3.org/2005/xpath-functions"; (: Need to backslash escape any double quotes, backslashes, and newlines :) declare function json:escape($s as xs:string) as xs:string { let $s := replace($s, "\\", "\\\\")
|
|
|
47 |
let $s := replace($s, """", "\\""") let $s := replace($s, codepoints-to-string((13, 10)), "\\n") let $s := replace($s, codepoints-to-string(13), "\\n") let $s := replace($s, codepoints-to-string(10),
|
|
|
48 |
"\\n") return $s }; declare function json:atomize($x as element()) as xs:string { if (count($x/node()) = 0) then 'null' else if ($x/@type = "number") then let $castable := $x castable as xs:float or $x castable as xs:double
|
|
|
49 |
or $x castable as xs:decimal return if ($castable) then xs:string($x) else error(concat("Not a number: ", xdmp:describe($x))) else if ($x/@type = "boolean") then let $castable := $x castable as xs:boolean return if ($castable)
|
|
|
50 |
then xs:string(xs:boolean($x)) else error(concat("Not a boolean: ", xdmp:describe($x))) else concat('"', json:escape($x), '"') }; (: Print the thing that comes after the colon :) declare function json:print-value($x as
|
|
|
51 |
element()) as xs:string { if (count($x/*) = 0) then json:atomize($x) else if ($x/@quote = "true") then concat('"', json:escape(xdmp:quote($x/node())), '"') else string-join(('{', string-join(for $i in $x/* return json:print-name-value($i),
|
|
|
52 |
","), '}'), "") }; (: Print the name and value both :) declare function json:print-name-value($x as element()) as xs:string? { let $name := name($x) let $first-in-array := count($x/preceding-sibling::*[name(.) = $name])
|
|
|
53 |
= 0 and (count($x/following-sibling::*[name(.) = $name]) > 0 or $x/@array = "true") let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) > 0 return if ($later-in-array) then () (: I was handled previously
|
|
|
54 |
:) else if ($first-in-array) then string-join(('"', json:escape($name), '":[', string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), ","), ']'), "") else string-join(('"',
|
|
|
55 |
json:escape($name), '":', json:print-value($x)), "") }; (:~ Transforms an XML element into a JSON string representation. See http://json.org. <p/> Sample usage: <pre> xquery version "1.0-ml"; import module
|
|
|
56 |
namespace json="http://marklogic.com/json" at "json.xqy"; json:serialize(&lt;foo&gt;&lt;bar&gt;kid&lt;/bar&gt;&lt;/foo&gt;) </pre> Sample transformations: <pre> &lt;e/&gt;
|
|
|
57 |
becomes {"e":null} &lt;e&gt;text&lt;/e&gt; becomes {"e":"text"} &lt;e&gt;quote " escaping&lt;/e&gt; becomes {"e":"quote \" escaping"} &lt;e&gt;backslash
|
|
|
58 |
\ escaping&lt;/e&gt; becomes {"e":"backslash \\ escaping"} &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"}}
|
|
|
59 |
&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"]}} &lt;e&gt;&lt;a array="true"&gt;text1&lt;/a&gt;&lt;/e&gt;
|
|
|
60 |
becomes {"e":{"a":["text1"]}} &lt;e&gt;&lt;a type="boolean"&gt;false&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":false}} &lt;e&gt;&lt;a type="number"&gt;123.5&lt;/a&gt;&lt;/e&gt;
|
|
|
61 |
becomes {"e":{"a":123.5}} &lt;e quote="true"&gt;&lt;div attrib="value"/&gt;&lt;/e&gt; becomes {"e":"&lt;div attrib=\"value\"/&gt;"} </pre>
|
|
|
62 |
<p/> Namespace URIs are ignored. Namespace prefixes are included in the JSON name. <p/> Attributes are ignored, except for the special attribute @array="true" that indicates the JSON serialization should write the node,
|
|
|
63 |
even if single, as an array, and the attribute @type that can be set to "boolean" or "number" to dictate the value should be written as that type (unquoted). There's also an @quote attribute that when set to true writes
|
|
|
64 |
the inner content as text rather than as structured JSON, useful for sending some XHTML over the wire. <p/> Text nodes within mixed content are ignored. @param $x Element node to convert @return String holding JSON serialized representation
|
|
|
65 |
of $x @author Jason Hunter @version 1.0.1 Ported to xquery 1.0-ml; double escaped backslashes in json:escape :) declare function json:serialize($x as element()) as xs:string { string-join(('{', json:print-name-value($x), '}'), "")
|
|
|
66 |
}; </textarea>
|
|
|
67 |
</div>
|
|
|
68 |
<script>
|
|
|
69 |
var editor = CodeMirror.fromTextArea(document.getElementById("code"),
|
|
|
70 |
{
|
|
|
71 |
lineNumbers: true,
|
|
|
72 |
matchBrackets: true,
|
|
|
73 |
theme: "xq-dark"
|
|
|
74 |
});
|
|
|
75 |
</script>
|
|
|
76 |
<p>
|
|
|
77 |
<strong>MIME types defined:</strong> <code>application/xquery</code>.</p>
|
|
|
78 |
<p>Development of the CodeMirror XQuery mode was sponsored by
|
|
|
79 |
<a href="http://marklogic.com">MarkLogic</a> and developed by
|
|
|
80 |
<a href="https://twitter.com/mbrevoort">Mike Brevoort</a>. </p>
|
|
|
81 |
</article>
|