Farabi

 view release on metacpan or  search on metacpan

lib/Farabi/files/public/assets/codemirror/mode/xquery/index.html  view on Meta::CPAN

declare function json:print-value($x as element()) as xs:string {
  if (count($x/*) = 0) then
    json:atomize($x)
  else if ($x/@quote = "true") then
    concat('"', json:escape(xdmp:quote($x/node())), '"')
  else
    string-join(('{',
      string-join(for $i in $x/* return json:print-name-value($i), ","),
    '}'), "")
};

(: Print the name and value both :)
declare function json:print-name-value($x as element()) as xs:string? {
  let $name := name($x)
  let $first-in-array :=
    count($x/preceding-sibling::*[name(.) = $name]) = 0 and
    (count($x/following-sibling::*[name(.) = $name]) > 0 or $x/@array = "true")
  let $later-in-array := count($x/preceding-sibling::*[name(.) = $name]) > 0
  return

  if ($later-in-array) then
    ()  (: I was handled previously :)
  else if ($first-in-array) then
    string-join(('"', json:escape($name), '":[',
      string-join((for $i in ($x, $x/following-sibling::*[name(.) = $name]) return json:print-value($i)), ","),
    ']'), "")
   else
     string-join(('"', json:escape($name), '":', json:print-value($x)), "")
};

(:~
  Transforms an XML element into a JSON string representation.  See http://json.org.
  <p/>
  Sample usage:
  <pre>
    xquery version "1.0-ml";
    import module namespace json="http://marklogic.com/json" at "json.xqy";
    json:serialize(<foo><bar>kid</bar></foo>)
  </pre>
  Sample transformations:
  <pre>
  <e/> becomes {"e":null}
  <e>text</e> becomes {"e":"text"}
  <e>quote " escaping</e> becomes {"e":"quote \" escaping"}
  <e>backslash \ escaping</e> becomes {"e":"backslash \\ escaping"}
  <e><a>text1</a><b>text2</b></e> becomes {"e":{"a":"text1","b":"text2"}}
  <e><a>text1</a><a>text2</a></e> becomes {"e":{"a":["text1","text2"]}}
  <e><a array="true">text1</a></e> becomes {"e":{"a":["text1"]}}
  <e><a type="boolean">false</a></e> becomes {"e":{"a":false}}
  <e><a type="number">123.5</a></e> becomes {"e":{"a":123.5}}
  <e quote="true"><div attrib="value"/></e> becomes {"e":"<div attrib=\"value\"/>"}
  </pre>
  <p/>
  Namespace URIs are ignored.  Namespace prefixes are included in the JSON name.
  <p/>
  Attributes are ignored, except for the special attribute @array="true" that
  indicates the JSON serialization should write the node, even if single, as an
  array, and the attribute @type that can be set to "boolean" or "number" to
  dictate the value should be written as that type (unquoted).  There's also
  an @quote attribute that when set to true writes the inner content as text
  rather than as structured JSON, useful for sending some XHTML over the
  wire.
  <p/>
  Text nodes within mixed content are ignored.

  @param $x Element node to convert
  @return String holding JSON serialized representation of $x

  @author Jason Hunter
  @version 1.0.1
  
  Ported to xquery 1.0-ml; double escaped backslashes in json:escape
:)
declare function json:serialize($x as element())  as xs:string {
  string-join(('{', json:print-name-value($x), '}'), "")
};
  </textarea> 
</div> 
 
    <script> 
      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        lineNumbers: true,
        matchBrackets: true,
        theme: "xq-dark"
      });
    </script> 
 
    <p><strong>MIME types defined:</strong> <code>application/xquery</code>.</p> 
 
    <p>Development of the CodeMirror XQuery mode was sponsored by 
      <a href="http://marklogic.com">MarkLogic</a> and developed by 
      <a href="https://twitter.com/mbrevoort">Mike Brevoort</a>.
    </p>
 
  </article>



( run in 1.006 second using v1.01-cache-2.11-cpan-5b529ec07f3 )