Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Created June 1, 2020 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ableasdale/35675988a15b75539712ace91d7890b6 to your computer and use it in GitHub Desktop.
Save ableasdale/35675988a15b75539712ace91d7890b6 to your computer and use it in GitHub Desktop.
MarkLogic: processing JSON using XPath
xquery version "1.0-ml";
(: Taken from:
https://www.linkedin.com/posts/kurtcagle_informatix8gulp-saxon-xslt-activity-6673109285721010176-hDYU/
See also:
https://www.npmjs.com/package/@informatix8/gulp-saxon-xslt
The above example was modified from Kurt's example to work with MarkLogic Server.
:)
let $json := '[
{"name":"Jane Doe","occupation":"Data Scientist","age":24},
{"name":"George Keens","occupation":"Programmer","age":27},
{"name":"Elaine Chou","occupation":"Manager","age":32}]'
let $records := xdmp:unquote($json)
let $avg-age := format-number(avg($records//age), '###.00')
return <div>The average age of all employees is <b>{$avg-age}</b>.</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment