XQuery mode
AخA
1
xquery version "1.0-ml"; (: this is : a "comment" :) let $let := <x attr="value">"test"<func>function() $var {function()} {$var}</func></x> let $joe:=1 return element element { attribute attribute
2
{ 1 }, element test { 'a' }, attribute foo { "bar" }, fn:doc()[ foo/@bar eq $let ], //x } (: a more 'evil' test :) (: Modified Blakeley example (: with nested comment :) ... :) declare private function local:declare() {()};
3
declare private function local:private() {()}; declare private function local:function() {()}; declare private function local:local() {()}; let $let := <let>let $let := "let"</let> return element element { attribute attribute
4
{ try { xdmp:version() } catch($e) { xdmp:log($e) } }, attribute fn:doc { "bar" castable as xs:string }, element text { text { "text" } }, fn:doc()[ child::eq/(@bar | attribute::attribute) eq $let ], //fn:doc } xquery version
5
"1.0-ml"; (: Copyright 2006-2010 Mark Logic Corporation. :) (: : Licensed under the Apache License, Version 2.0 (the "License"); : you may not use this file except in compliance with the License. : You may obtain a copy
6
of the License at : : http://www.apache.org/licenses/LICENSE-2.0 : : Unless required by applicable law or agreed to in writing, software : distributed under the License is distributed on an "AS IS" BASIS, : WITHOUT WARRANTIES OR
7
CONDITIONS OF ANY KIND, either express or implied. : See the License for the specific language governing permissions and : limitations under the License. :) module namespace json = "http://marklogic.com/json"; declare default function
8
namespace "http://www.w3.org/2005/xpath-functions"; (: Need to backslash escape any double quotes, backslashes, and newlines :) declare function json:escape($s as xs:string) as xs:string { let $s := replace($s, "\\", "\\\\")
9
let $s := replace($s, """", "\\""") let $s := replace($s, codepoints-to-string((13, 10)), "\\n") let $s := replace($s, codepoints-to-string(13), "\\n") let $s := replace($s, codepoints-to-string(10),
10
"\\n") return $s }; declare function json:atomize($x as element()) as xs:string { if (count($x/node()) = 0) then 'null' else if ($x/@type = "number") then let $castable := $x castable as xs:float or $x castable as xs:double
11
or $x castable as xs:decimal return if ($castable) then xs:string($x) else error(concat("Not a number: ", xdmp:describe($x))) else if ($x/@type = "boolean") then let $castable := $x castable as xs:boolean return if ($castable)
12
then xs:string(xs:boolean($x)) else error(concat("Not a boolean: ", xdmp:describe($x))) else concat('"', json:escape($x), '"') }; (: Print the thing that comes after the colon :) declare function json:print-value($x as
13
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),
14
","), '}'), "") }; (: 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])
15
= 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
16
:) 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(('"',
17
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
18
namespace json="http://marklogic.com/json" at "json.xqy"; json:serialize(<foo><bar>kid</bar></foo>) </pre> Sample transformations: <pre> <e/>
19
becomes {"e":null} <e>text</e> becomes {"e":"text"} <e>quote " escaping</e> becomes {"e":"quote \" escaping"} <e>backslash
20
\ escaping</e> becomes {"e":"backslash \\ escaping"} <e><a>text1</a><b>text2</b></e> becomes {"e":{"a":"text1","b":"text2"}}
21
<e><a>text1</a><a>text2</a></e> becomes {"e":{"a":["text1","text2"]}} <e><a array="true">text1</a></e>
22
becomes {"e":{"a":["text1"]}} <e><a type="boolean">false</a></e> becomes {"e":{"a":false}} <e><a type="number">123.5</a></e>
23
becomes {"e":{"a":123.5}} <e quote="true"><div attrib="value"/></e> becomes {"e":"<div attrib=\"value\"/>"} </pre>
24
<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,
25
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
26
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
27
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), '}'), "")
28
};
MIME types defined: application/xquery
.
Development of the CodeMirror XQuery mode was sponsored by MarkLogic and developed by Mike Brevoort.