openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

index.html (8609B)


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