Skip to content

Instantly share code, notes, and snippets.

View tternquist's full-sized avatar

Tom Ternquist tternquist

View GitHub Profile
sem:sparql("PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
SELECT ?doc {?s <isDefinedBy> ?o
BIND(fn:doc(?o) as ?doc)}") ! map:get(.,"doc")
@tternquist
tternquist / batch-processing-with-spawn-function.xqy
Last active May 31, 2017 17:03
Batch processing in marklogic with spawn function
xquery version "1.0-ml";
let $input := for $i in (1 to 100) return $i
let $batch-size := 2
let $input-size := fn:count($input)
let $num-batches := xs:int(math:ceil($input-size div $batch-size ))
let $result :=
for $batch-start in (1 to $num-batches)
let $processing-seq := $input[($batch-size * ($batch-start - 1) + 1) to ($batch-size * ($batch-start ))]
return
xdmp:spawn-function(function() {
@tternquist
tternquist / root-element-query.xqy
Last active December 22, 2016 23:09
Query on root element in MarkLogic
declare namespace qry = "http://marklogic.com/cts/query";
let $root-element := "*:myRootElem"
return
cts:term-query(xdmp:plan(/xdmp:unpath($root-element))//qry:final-plan//qry:term-query/qry:key)
@tternquist
tternquist / gzip-encoding.xqy
Created December 22, 2016 23:01
Gzip encoding for MarkLogic REST extension
let $result := element result {"Hi"}
return
if(fn:contains(xdmp:get-request-header("Accept-encoding"),"gzip"))
then
(
document {
xdmp:gzip(
text { $result } )
},
xdmp:add-response-header("Content-encoding", "gzip")
@tternquist
tternquist / dynamic-xpath-expression.xqy
Created December 22, 2016 22:55
Perform a dynamic xpath expression in MarkLogic
let $xml := <hi><test><inner>test</inner></test></hi>
return $xml/xdmp:unpath("/test/inner")
let $test := element outer {
attribute at {1},
element a { "a" },
element b { "b" },
element c { "c" }
}
return
element outer {
$test/(node()|@*)[. except $test/c]
@tternquist
tternquist / ml-permissions-role-name.xqy
Created December 22, 2016 22:41
Get role name and permissions for MarkLogic document
import module namespace sec="http://marklogic.com/xdmp/security" at
"/MarkLogic/security.xqy";
declare function local:document-get-permissions($uri) {
xdmp:document-get-permissions($uri) !
xdmp:invoke-function(function() {
element role {.,
sec:get-role-names(xs:unsignedLong(.//sec:role-id ))
}
},<options xmlns="xdmp:eval"><database>{xdmp:database("Security")}</database></options>)
};
@tternquist
tternquist / sum-maps.xqy
Created November 23, 2016 12:48
Sum maps in XQuery (1.0-ml)
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
let $existing := map:entry("foo",9)
let $maps := (
map:entry("foo",1),
map:entry("foo",2),
map:entry("foo",3),
map:entry("foo",4),
map:entry("foo",5),
map:entry("foo",1)
@tternquist
tternquist / marklogic-uri-range-index.xqy
Created November 23, 2016 12:46
Range index for document URIs
cts:element-range-query(xs:QName("xdmp:document"),"=","myUri.xml","collation=http://marklogic.com/collation/codepoint")
@tternquist
tternquist / bitemp-as-of-time.xqy
Created December 16, 2015 19:17
Get documents that were in system as of time
let $time := xs:dateTime("2015-12-16T17:30:00Z")
let $period := cts:period($time)
let $query := cts:period-range-query("system","ALN_CONTAINS", $period)
return cts:search(/,$query)