Skip to content

Instantly share code, notes, and snippets.

@ryanjdew
ryanjdew / hardened-xquery.xqy
Created May 23, 2013 13:52
Example of dynamically created XQuery that has been hardened.
xquery version "1.0-ml";
declare function local:prolog($element as element(), $position as xs:integer) {
if ($element instance of element(value))
then
fn:string-join(
local:prolog-var($element/*, $position),
"
"
)
@ryanjdew
ryanjdew / unsecure-xquery.xqy
Created May 23, 2013 13:51
This is an example of bad XQuery that could result in XQuery Injection
xquery version "1.0-ml";
declare function local:prolog($element as element()) {
if ($element instance of element(value))
then
fn:string-join(
local:prolog-ns($element/*),
"
"
)
@ryanjdew
ryanjdew / good-bool.xqy
Created February 23, 2013 14:21
Good examples of boolean expressions
let $items as xs:string* := ('test', 'values', 'to', 'iterate', 'over')
$bool1 as xs:boolean := fn:exists($items[. ne '']),
$bool2 as xs:boolean := fn:exists(fn:subsequence($items,6)),
$bool3 as xs:boolean := some $i in $items satisfies $i eq "some-value",
$bool4 as xs:boolean := every $i in $items satisfies $i instance of xs:string
return (
if ($bool1)
then "bool1: true"
else (),
@ryanjdew
ryanjdew / bad-bool.xqy
Created February 21, 2013 13:53
Bad XQuery Boolean Examples
let $items as xs:string* := ('test', 'values', 'to', 'iterate', 'over')
$bool1 as xs:boolean := if (fn:exists($items)) then fn:true() else fn:false(),
$bool2 as xs:boolean? := if (fn:exists(fn:subsequence($items,6))) then fn:true() else (),
$bool3 as xs:boolean* := for $i in $items return $i eq "some-value",
$bool4 as xs:boolean* := for $i in $items return $i eq "iterate"
return (
if ($bool1)
then "bool1: true"
else (),
@ryanjdew
ryanjdew / transform.xqy
Last active December 13, 2015 20:18
A different way to transform content in MarkLogic.
xquery version "1.0-ml";
import module namespace mem = "http://maxdewpoint.blogspot.com/memory-operations" at "/memory-operations.xqy";
(: Functions for transforming xml :)
declare function local:bar($node as node()) as node()*
{
<barr>{$node/node()}</barr>
};
declare function local:baz($node as node()) as node()*
{
<bazz>{$node/node()}</bazz>
@ryanjdew
ryanjdew / sliding-window-ml6.xqy
Created February 15, 2013 13:56
Sliding window implementation using MarkLogic 6.
xquery version "1.0-ml";
declare function local:sliding-window(
$sequence as item()*,
$only-start as xs:boolean,
$start-condition as function(*),
$only-end as xs:boolean,
$end-condition as function(*),
$return as function(*)
) {
@ryanjdew
ryanjdew / tumbling-window-ml6.xqy
Created February 14, 2013 23:26
Revisiting an implementation of tumbling window, this time making use of inline functions and the MarkLogic map:map object.
xquery version "1.0-ml";
declare function local:tumbling-window(
$sequence as item()*,
$only-start as xs:boolean,
$start-condition as function(*),
$only-end as xs:boolean,
$end-condition as function(*),
$return as function(*)
) {
@ryanjdew
ryanjdew / unob.xqy
Created February 2, 2013 21:16
One way to write unobtrusive XQuery code for HTML replacement
let $html as element(html) := html:html-for-page($page-uri),
$content-for-page := content:page-content($page-uri),
$transaction-id := mem:copy($html),
$binding-lambda := function ($html, $content) {
for $prop in $html/descendant-or-self::*[@itemprop]
let $prop-name := fn:string($prop/@itemprop),
$prop-value := $content/descendant-or-self::*[@itemprop eq $prop-name]/node()
return
if ($prop-name eq 'url')
then mem:replace-value($transaction-id, ($prop/(@href|@src|@data))[1], $prop-value)
@ryanjdew
ryanjdew / from-json-transform.xqy
Last active October 19, 2018 16:27
The following makes xdmp:from-json work the same in ML 5 and ML 6
xquery version "1.0-ml";
declare namespace json = 'http://marklogic.com/xdmp/json';
declare function local:normalize-from-json($json as item()) {
if (fn:type-available('json:object'))
then local:_normalize-from-json($json)
else $json
};
@ryanjdew
ryanjdew / find-empty-dirs.xqy
Created January 7, 2013 04:44
This can help you to find empty directories if the uri lexicon is enabled in MarkLogic.
xquery version "1.0-ml";
let $directories-map := (cts:uris('/',('properties','map'),cts:directory-query('/','infinity'))
-
cts:uris('/',('document','map'),cts:directory-query('/','infinity')))
return map:keys($directories-map)[xdmp:estimate(cts:search(fn:collection(),cts:directory-query(.,'infinity'))) eq 0]