Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
xquery version "3.0";
(: the power of algebraic data types in xquery
This example shows how we can composite up data models
which 'carry' their own operations along with them.
(: using John Snelson's inspirational :)
import module namespace data="" at "data.xq";
(: one day we create a person data model :)
declare variable $person := data:declare(
(: some time later we realise we need an address :)
declare variable $address := data:declare(
(: example of providing custom input validation :)
declare function local:set-city(
if($city ne "London")
then error(xs:QName('BADCITY'))
else $address[2]($city)
(: now for the magic, algebraic data type lets us easily compose data models :)
let $record := ($person,$address)
(: data:declare has already provided us the accessors :)
let $me := (
$record[1]("Jim Fuller"),
$record[2]("Station road"),
local:set-city("London"), (:as an example of providing custom validation:)
$record[4]("N13 9PR")
(: direct access values:)
$me[1](function($v){"Full Name: " || $v}), (: custom output :)
(: useful data: functions for describing type and structure:)
$me ! (data:type(.),data:describe(.)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.