annotate rhodecode/public/js/mode/xquery/index.html @ 4029:c9bcfe2d2ade

codecleaner
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 21 Jun 2013 01:11:16 +0200
parents a60a0e9092c6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
1 <!doctype html>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
2 <html>
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 <!--
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 /*
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 Copyright (C) 2011 by MarkLogic Corporation
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 Author: Mike Brevoort <mike@brevoort.com>
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 Permission is hereby granted, free of charge, to any person obtaining a copy
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 of this software and associated documentation files (the "Software"), to deal
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 in the Software without restriction, including without limitation the rights
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 copies of the Software, and to permit persons to whom the Software is
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 furnished to do so, subject to the following conditions:
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 The above copyright notice and this permission notice shall be included in
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 all copies or substantial portions of the Software.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 THE SOFTWARE.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 */
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 -->
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
27 <head>
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 <meta charset="utf-8">
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
29 <title>CodeMirror: XQuery mode</title>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
30 <link rel="stylesheet" href="../../lib/codemirror.css">
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
31 <script src="../../lib/codemirror.js"></script>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
32 <script src="xquery.js"></script>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
33 <link rel="stylesheet" href="../../doc/docs.css">
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
34 <link rel="stylesheet" href="../../theme/xq-dark.css">
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 <style type="text/css">
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
36 .CodeMirror {
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
37 border-top: 1px solid black; border-bottom: 1px solid black;
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
38 height:400px;
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
39 }
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
40 </style>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
41 </head>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
42 <body>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
43 <h1>CodeMirror: XQuery mode</h1>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
44
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
45 <div class="cm-s-default">
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
46 <textarea id="code" name="code">
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 xquery version &quot;1.0-ml&quot;;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 (: this is
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
49 : a
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 "comment" :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 let $let := &lt;x attr=&quot;value&quot;&gt;&quot;test&quot;&lt;func&gt;function() $var {function()} {$var}&lt;/func&gt;&lt;/x&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 let $joe:=1
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 return element element {
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
54 attribute attribute { 1 },
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
55 element test { &#39;a&#39; },
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
56 attribute foo { &quot;bar&quot; },
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
57 fn:doc()[ foo/@bar eq $let ],
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
58 //x }
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
59
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 (: a more 'evil' test :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 (: Modified Blakeley example (: with nested comment :) ... :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 declare private function local:declare() {()};
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 declare private function local:private() {()};
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 declare private function local:function() {()};
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 declare private function local:local() {()};
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 let $let := &lt;let&gt;let $let := &quot;let&quot;&lt;/let&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 return element element {
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
68 attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } },
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
69 attribute fn:doc { &quot;bar&quot; castable as xs:string },
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
70 element text { text { &quot;text&quot; } },
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
71 fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ],
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
72 //fn:doc
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 }
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 xquery version &quot;1.0-ml&quot;;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 (: Copyright 2006-2010 Mark Logic Corporation. :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 (:
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 : Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 : you may not use this file except in compliance with the License.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 : You may obtain a copy of the License at
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 :
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 : http://www.apache.org/licenses/LICENSE-2.0
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 :
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 : Unless required by applicable law or agreed to in writing, software
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 : distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 : See the License for the specific language governing permissions and
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 : limitations under the License.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 module namespace json = &quot;http://marklogic.com/json&quot;;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 declare default function namespace &quot;http://www.w3.org/2005/xpath-functions&quot;;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 (: Need to backslash escape any double quotes, backslashes, and newlines :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 declare function json:escape($s as xs:string) as xs:string {
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 let $s := replace($s, &quot;\\&quot;, &quot;\\\\&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 let $s := replace($s, &quot;&quot;&quot;&quot;, &quot;\\&quot;&quot;&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 let $s := replace($s, codepoints-to-string((13, 10)), &quot;\\n&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 let $s := replace($s, codepoints-to-string(13), &quot;\\n&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 let $s := replace($s, codepoints-to-string(10), &quot;\\n&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 return $s
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 };
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 declare function json:atomize($x as element()) as xs:string {
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 if (count($x/node()) = 0) then 'null'
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 else if ($x/@type = &quot;number&quot;) then
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 let $castable := $x castable as xs:float or
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 $x castable as xs:double or
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 $x castable as xs:decimal
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 return
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 if ($castable) then xs:string($x)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 else error(concat(&quot;Not a number: &quot;, xdmp:describe($x)))
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 else if ($x/@type = &quot;boolean&quot;) then
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 let $castable := $x castable as xs:boolean
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 return
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 if ($castable) then xs:string(xs:boolean($x))
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 else error(concat(&quot;Not a boolean: &quot;, xdmp:describe($x)))
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 else concat('&quot;', json:escape($x), '&quot;')
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 };
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 (: Print the thing that comes after the colon :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 declare function json:print-value($x as element()) as xs:string {
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 if (count($x/*) = 0) then
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 json:atomize($x)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 else if ($x/@quote = &quot;true&quot;) then
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 concat('&quot;', json:escape(xdmp:quote($x/node())), '&quot;')
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 else
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 string-join(('{',
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 string-join(for $i in $x/* return json:print-name-value($i), &quot;,&quot;),
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 '}'), &quot;&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 };
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 (: Print the name and value both :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 declare function json:print-name-value($x as element()) as xs:string? {
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 let $name := name($x)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 let $first-in-array :=
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 count($x/preceding-sibling::*[name(.) = $name]) = 0 and
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 (count($x/following-sibling::*[name(.) = $name]) &gt; 0 or $x/@array = &quot;true&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) &gt; 0
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 return
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 if ($later-in-array) then
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 () (: I was handled previously :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 else if ($first-in-array) then
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 string-join(('&quot;', json:escape($name), '&quot;:[',
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), &quot;,&quot;),
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 ']'), &quot;&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 else
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 string-join(('&quot;', json:escape($name), '&quot;:', json:print-value($x)), &quot;&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 };
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 (:~
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 Transforms an XML element into a JSON string representation. See http://json.org.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 &lt;p/&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 Sample usage:
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 &lt;pre&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 xquery version &quot;1.0-ml&quot;;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 import module namespace json=&quot;http://marklogic.com/json&quot; at &quot;json.xqy&quot;;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 json:serialize(&amp;lt;foo&amp;gt;&amp;lt;bar&amp;gt;kid&amp;lt;/bar&amp;gt;&amp;lt;/foo&amp;gt;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 &lt;/pre&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 Sample transformations:
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 &lt;pre&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 &amp;lt;e/&amp;gt; becomes {&quot;e&quot;:null}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 &amp;lt;e&amp;gt;text&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;text&quot;}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 &amp;lt;e&amp;gt;quote &quot; escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;quote \&quot; escaping&quot;}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 &amp;lt;e&amp;gt;backslash \ escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;backslash \\ escaping&quot;}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 &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;}}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 &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;]}}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 &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;]}}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 &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}}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 &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}}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 &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;}
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 &lt;/pre&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 &lt;p/&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 Namespace URIs are ignored. Namespace prefixes are included in the JSON name.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 &lt;p/&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 Attributes are ignored, except for the special attribute @array=&quot;true&quot; that
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 indicates the JSON serialization should write the node, even if single, as an
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 array, and the attribute @type that can be set to &quot;boolean&quot; or &quot;number&quot; to
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 dictate the value should be written as that type (unquoted). There's also
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 an @quote attribute that when set to true writes the inner content as text
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 rather than as structured JSON, useful for sending some XHTML over the
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 wire.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 &lt;p/&gt;
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 Text nodes within mixed content are ignored.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 @param $x Element node to convert
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192 @return String holding JSON serialized representation of $x
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194 @author Jason Hunter
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 @version 1.0.1
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
196
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 Ported to xquery 1.0-ml; double escaped backslashes in json:escape
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198 :)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 declare function json:serialize($x as element()) as xs:string {
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200 string-join(('{', json:print-name-value($x), '}'), &quot;&quot;)
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 };
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
202 </textarea>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
203 </div>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
204
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
205 <script>
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 lineNumbers: true,
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 matchBrackets: true,
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 theme: "xq-dark"
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 });
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
211 </script>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
212
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
213 <p><strong>MIME types defined:</strong> <code>application/xquery</code>.</p>
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
214
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
215 <p>Development of the CodeMirror XQuery mode was sponsored by
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
216 <a href="http://marklogic.com">MarkLogic</a> and developed by
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217 <a href="https://twitter.com/mbrevoort">Mike Brevoort</a>.
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 </p>
4029
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
219
c9bcfe2d2ade codecleaner
Marcin Kuzminski <marcin@python-works.com>
parents: 4026
diff changeset
220 </body>
4026
a60a0e9092c6 added codemirror edit mode with autodetection
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221 </html>