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(&lt;foo&gt;&lt;bar&gt;kid&lt;/bar&gt;&lt;/foo&gt;)
</pre>
Sample transformations:
<pre>
&lt;e/&gt; becomes {"e":null}
&lt;e&gt;text&lt;/e&gt; becomes {"e":"text"}
&lt;e&gt;quote " escaping&lt;/e&gt; becomes {"e":"quote \" escaping"}
&lt;e&gt;backslash \ escaping&lt;/e&gt; becomes {"e":"backslash \\ escaping"}
&lt;e&gt;&lt;a&gt;text1&lt;/a&gt;&lt;b&gt;text2&lt;/b&gt;&lt;/e&gt; becomes {"e":{"a":"text1","b":"text2"}}
&lt;e&gt;&lt;a&gt;text1&lt;/a&gt;&lt;a&gt;text2&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1","text2"]}}
&lt;e&gt;&lt;a array="true"&gt;text1&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":["text1"]}}
&lt;e&gt;&lt;a type="boolean"&gt;false&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":false}}
&lt;e&gt;&lt;a type="number"&gt;123.5&lt;/a&gt;&lt;/e&gt; becomes {"e":{"a":123.5}}
&lt;e quote="true"&gt;&lt;div attrib="value"/&gt;&lt;/e&gt; becomes {"e":"&lt;div attrib=\"value\"/&gt;"}
</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 )