You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
8.4 KiB

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