index.html 9.2 KB

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