index.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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="../../addon/edit/matchbrackets.js"></script>
  9. <script src="xquery.js"></script>
  10. <style type="text/css">
  11. .CodeMirror {
  12. border-top: 1px solid black; border-bottom: 1px solid black;
  13. height:400px;
  14. }
  15. </style>
  16. <div id=nav>
  17. <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
  18. <ul>
  19. <li><a href="../../index.html">Home</a>
  20. <li><a href="../../doc/manual.html">Manual</a>
  21. <li><a href="https://github.com/codemirror/codemirror">Code</a>
  22. </ul>
  23. <ul>
  24. <li><a href="../index.html">Language modes</a>
  25. <li><a class=active href="#">XQuery</a>
  26. </ul>
  27. </div>
  28. <article>
  29. <h2>XQuery mode</h2>
  30. <div class="cm-s-default">
  31. <textarea id="code" name="code">
  32. xquery version &quot;1.0-ml&quot;;
  33. (: this is
  34. : a
  35. "comment" :)
  36. 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;
  37. let $joe:=1
  38. return element element {
  39. attribute attribute { 1 },
  40. element test { &#39;a&#39; },
  41. attribute foo { &quot;bar&quot; },
  42. fn:doc()[ foo/@bar eq $let ],
  43. //x }
  44. (: a more 'evil' test :)
  45. (: Modified Blakeley example (: with nested comment :) ... :)
  46. declare private function local:declare() {()};
  47. declare private function local:private() {()};
  48. declare private function local:function() {()};
  49. declare private function local:local() {()};
  50. let $let := &lt;let&gt;let $let := &quot;let&quot;&lt;/let&gt;
  51. return element element {
  52. attribute attribute { try { xdmp:version() } catch($e) { xdmp:log($e) } },
  53. attribute fn:doc { &quot;bar&quot; castable as xs:string },
  54. element text { text { &quot;text&quot; } },
  55. fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ],
  56. //fn:doc
  57. }
  58. xquery version &quot;1.0-ml&quot;;
  59. (: Copyright 2006-2010 Mark Logic Corporation. :)
  60. (:
  61. : Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
  62. : you may not use this file except in compliance with the License.
  63. : You may obtain a copy of the License at
  64. :
  65. : http://www.apache.org/licenses/LICENSE-2.0
  66. :
  67. : Unless required by applicable law or agreed to in writing, software
  68. : distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
  69. : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  70. : See the License for the specific language governing permissions and
  71. : limitations under the License.
  72. :)
  73. module namespace json = &quot;http://marklogic.com/json&quot;;
  74. declare default function namespace &quot;http://www.w3.org/2005/xpath-functions&quot;;
  75. (: Need to backslash escape any double quotes, backslashes, and newlines :)
  76. declare function json:escape($s as xs:string) as xs:string {
  77. let $s := replace($s, &quot;\\&quot;, &quot;\\\\&quot;)
  78. let $s := replace($s, &quot;&quot;&quot;&quot;, &quot;\\&quot;&quot;&quot;)
  79. let $s := replace($s, codepoints-to-string((13, 10)), &quot;\\n&quot;)
  80. let $s := replace($s, codepoints-to-string(13), &quot;\\n&quot;)
  81. let $s := replace($s, codepoints-to-string(10), &quot;\\n&quot;)
  82. return $s
  83. };
  84. declare function json:atomize($x as element()) as xs:string {
  85. if (count($x/node()) = 0) then 'null'
  86. else if ($x/@type = &quot;number&quot;) then
  87. let $castable := $x castable as xs:float or
  88. $x castable as xs:double or
  89. $x castable as xs:decimal
  90. return
  91. if ($castable) then xs:string($x)
  92. else error(concat(&quot;Not a number: &quot;, xdmp:describe($x)))
  93. else if ($x/@type = &quot;boolean&quot;) then
  94. let $castable := $x castable as xs:boolean
  95. return
  96. if ($castable) then xs:string(xs:boolean($x))
  97. else error(concat(&quot;Not a boolean: &quot;, xdmp:describe($x)))
  98. else concat('&quot;', json:escape($x), '&quot;')
  99. };
  100. (: Print the thing that comes after the colon :)
  101. declare function json:print-value($x as element()) as xs:string {
  102. if (count($x/*) = 0) then
  103. json:atomize($x)
  104. else if ($x/@quote = &quot;true&quot;) then
  105. concat('&quot;', json:escape(xdmp:quote($x/node())), '&quot;')
  106. else
  107. string-join(('{',
  108. string-join(for $i in $x/* return json:print-name-value($i), &quot;,&quot;),
  109. '}'), &quot;&quot;)
  110. };
  111. (: Print the name and value both :)
  112. declare function json:print-name-value($x as element()) as xs:string? {
  113. let $name := name($x)
  114. let $first-in-array :=
  115. count($x/preceding-sibling::*[name(.) = $name]) = 0 and
  116. (count($x/following-sibling::*[name(.) = $name]) &gt; 0 or $x/@array = &quot;true&quot;)
  117. let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) &gt; 0
  118. return
  119. if ($later-in-array) then
  120. () (: I was handled previously :)
  121. else if ($first-in-array) then
  122. string-join(('&quot;', json:escape($name), '&quot;:[',
  123. string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), &quot;,&quot;),
  124. ']'), &quot;&quot;)
  125. else
  126. string-join(('&quot;', json:escape($name), '&quot;:', json:print-value($x)), &quot;&quot;)
  127. };
  128. (:~
  129. Transforms an XML element into a JSON string representation. See http://json.org.
  130. &lt;p/&gt;
  131. Sample usage:
  132. &lt;pre&gt;
  133. xquery version &quot;1.0-ml&quot;;
  134. import module namespace json=&quot;http://marklogic.com/json&quot; at &quot;json.xqy&quot;;
  135. json:serialize(&amp;lt;foo&amp;gt;&amp;lt;bar&amp;gt;kid&amp;lt;/bar&amp;gt;&amp;lt;/foo&amp;gt;)
  136. &lt;/pre&gt;
  137. Sample transformations:
  138. &lt;pre&gt;
  139. &amp;lt;e/&amp;gt; becomes {&quot;e&quot;:null}
  140. &amp;lt;e&amp;gt;text&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;text&quot;}
  141. &amp;lt;e&amp;gt;quote &quot; escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;quote \&quot; escaping&quot;}
  142. &amp;lt;e&amp;gt;backslash \ escaping&amp;lt;/e&amp;gt; becomes {&quot;e&quot;:&quot;backslash \\ escaping&quot;}
  143. &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;}}
  144. &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;]}}
  145. &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;]}}
  146. &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}}
  147. &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}}
  148. &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;}
  149. &lt;/pre&gt;
  150. &lt;p/&gt;
  151. Namespace URIs are ignored. Namespace prefixes are included in the JSON name.
  152. &lt;p/&gt;
  153. Attributes are ignored, except for the special attribute @array=&quot;true&quot; that
  154. indicates the JSON serialization should write the node, even if single, as an
  155. array, and the attribute @type that can be set to &quot;boolean&quot; or &quot;number&quot; to
  156. dictate the value should be written as that type (unquoted). There's also
  157. an @quote attribute that when set to true writes the inner content as text
  158. rather than as structured JSON, useful for sending some XHTML over the
  159. wire.
  160. &lt;p/&gt;
  161. Text nodes within mixed content are ignored.
  162. @param $x Element node to convert
  163. @return String holding JSON serialized representation of $x
  164. @author Jason Hunter
  165. @version 1.0.1
  166. Ported to xquery 1.0-ml; double escaped backslashes in json:escape
  167. :)
  168. declare function json:serialize($x as element()) as xs:string {
  169. string-join(('{', json:print-name-value($x), '}'), &quot;&quot;)
  170. };
  171. </textarea>
  172. </div>
  173. <script>
  174. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  175. lineNumbers: true,
  176. matchBrackets: true,
  177. theme: "xq-dark"
  178. });
  179. </script>
  180. <p><strong>MIME types defined:</strong> <code>application/xquery</code>.</p>
  181. <p>Development of the CodeMirror XQuery mode was sponsored by
  182. <a href="http://marklogic.com">MarkLogic</a> and developed by
  183. <a href="https://twitter.com/mbrevoort">Mike Brevoort</a>.
  184. </p>
  185. </article>