仰望星辰工作室

better-staridc-MNBT

Z zfhsh first commit 2 个月前
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/5"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></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/codemirror5">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">
38xquery version &quot;1.0-ml&quot;;
39(: this is
40 : a
41 "comment" :)
42let $let := &lt;x attr=&quot;value&quot;&gt;&quot;test&quot;&lt;func&gt;function() $var {function()} {$var}&lt;/func&gt;&lt;/x&gt;
43let $joe:=1
44return element element {
45 attribute attribute { 1 },
46 element test { &#39;a&#39; },
47 attribute foo { &quot;bar&quot; },
48 fn:doc()[ foo/@bar eq $let ],
49 //x }
50
51(: a more 'evil' test :)
52(: Modified Blakeley example (: with nested comment :) ... :)
53declare private function local:declare() {()};
54declare private function local:private() {()};
55declare private function local:function() {()};
56declare private function local:local() {()};
57let $let := &lt;let&gt;let $let := &quot;let&quot;&lt;/let&gt;
58return element element {
59 attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } },
60 attribute fn:doc { &quot;bar&quot; castable as xs:string },
61 element text { text { &quot;text&quot; } },
62 fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ],
63 //fn:doc
64}
65
66
67
68xquery version &quot;1.0-ml&quot;;
69
70(: Copyright 2006-2010 Mark Logic Corporation. :)
71
72(:
73 : Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
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 &quot;AS IS&quot; 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
86module namespace json = &quot;http://marklogic.com/json&quot;;
87declare default function namespace &quot;http://www.w3.org/2005/xpath-functions&quot;;
88
89(: Need to backslash escape any double quotes, backslashes, and newlines :)
90declare function json:escape($s as xs:string) as xs:string {
91 let $s := replace($s, &quot;\\&quot;, &quot;\\\\&quot;)
92 let $s := replace($s, &quot;&quot;&quot;&quot;, &quot;\\&quot;&quot;&quot;)
93 let $s := replace($s, codepoints-to-string((13, 10)), &quot;\\n&quot;)
94 let $s := replace($s, codepoints-to-string(13), &quot;\\n&quot;)
95 let $s := replace($s, codepoints-to-string(10), &quot;\\n&quot;)
96 return $s
97};
98
99declare function json:atomize($x as element()) as xs:string {
100 if (count($x/node()) = 0) then 'null'
101 else if ($x/@type = &quot;number&quot;) 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(&quot;Not a number: &quot;, xdmp:describe($x)))
108 else if ($x/@type = &quot;boolean&quot;) then
109 let $castable := $x castable as xs:boolean
110 return
111 if ($castable) then xs:string(xs:boolean($x))
112 else error(concat(&quot;Not a boolean: &quot;, xdmp:describe($x)))
113 else concat('&quot;', json:escape($x), '&quot;')
114};
115
116(: Print the thing that comes after the colon :)
117declare 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 = &quot;true&quot;) then
121 concat('&quot;', json:escape(xdmp:quote($x/node())), '&quot;')
122 else
123 string-join(('{',
124 string-join(for $i in $x/* return json:print-name-value($i), &quot;,&quot;),
125 '}'), &quot;&quot;)
126};
127
128(: Print the name and value both :)
129declare 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]) &gt; 0 or $x/@array = &quot;true&quot;)
134 let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) &gt; 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(('&quot;', json:escape($name), '&quot;:[',
141 string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), &quot;,&quot;),
142 ']'), &quot;&quot;)
143 else
144 string-join(('&quot;', json:escape($name), '&quot;:', json:print-value($x)), &quot;&quot;)
145};
146
147(:~
148 Transforms an XML element into a JSON string representation. See http://json.org.
149 &lt;p/&gt;
150 Sample usage:
151 &lt;pre&gt;
152 xquery version &quot;1.0-ml&quot;;
153 import module namespace json=&quot;http://marklogic.com/json&quot; at &quot;json.xqy&quot;;
154 json:serialize(&amp;lt;foo&amp;gt;&amp;lt;bar&amp;gt;kid&amp;lt;/bar&amp;gt;&amp;lt;/foo&amp;gt;)
155 &lt;/pre&gt;
156 Sample transformations:
157 &lt;pre&gt;
158 &amp;lt;e/&amp;gt; becomes {&quot;e&quot;:null}
159 &amp;lt;e&amp;gt;text&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;text&quot;}
160 &amp;lt;e&amp;gt;quote &quot; escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;quote \&quot; escaping&quot;}
161 &amp;lt;e&amp;gt;backslash \ escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;backslash \\ escaping&quot;}
162 &amp;lt;e&amp;gt;&amp;lt;a&amp;gt;text1&amp;lt;/a&amp;gt;&amp;lt;b&amp;gt;text2&amp;lt;/b&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:&quot;text1&quot;,&quot;b&quot;:&quot;text2&quot;}}
163 &amp;lt;e&amp;gt;&amp;lt;a&amp;gt;text1&amp;lt;/a&amp;gt;&amp;lt;a&amp;gt;text2&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:[&quot;text1&quot;,&quot;text2&quot;]}}
164 &amp;lt;e&amp;gt;&amp;lt;a array=&quot;true&quot;&amp;gt;text1&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:[&quot;text1&quot;]}}
165 &amp;lt;e&amp;gt;&amp;lt;a type=&quot;boolean&quot;&amp;gt;false&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:false}}
166 &amp;lt;e&amp;gt;&amp;lt;a type=&quot;number&quot;&amp;gt;123.5&amp;lt;/a&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:{&quot;a&quot;:123.5}}
167 &amp;lt;e quote=&quot;true&quot;&amp;gt;&amp;lt;div attrib=&quot;value&quot;/&amp;gt;&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;&amp;lt;div attrib=\&quot;value\&quot;/&amp;gt;&quot;}
168 &lt;/pre&gt;
169 &lt;p/&gt;
170 Namespace URIs are ignored. Namespace prefixes are included in the JSON name.
171 &lt;p/&gt;
172 Attributes are ignored, except for the special attribute @array=&quot;true&quot; 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 &quot;boolean&quot; or &quot;number&quot; 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 &lt;p/&gt;
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:)
190declare function json:serialize($x as element()) as xs:string {
191 string-join(('{', json:print-name-value($x), '}'), &quot;&quot;)
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>