Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active November 24, 2015 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CliffordAnderson/1e3958444680dbb15a95 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/1e3958444680dbb15a95 to your computer and use it in GitHub Desktop.
Facets with Eval
xquery version "3.1";
(: Produces facets dynamically by applying a config file ('facets.xml')
to a book list ('books.csv') :)
declare function local:build-facet ($books as element()*, $path as xs:string, $name as xs:string) as element(facet)?
{
let $facets :=
for $book in $books
group by $facet :=
let $vars := map { '': document {$book}} (: Makes $book the context node :)
return xquery:eval($path, $vars) (: Evals the $path (xs:string) as an XPath expression :)
let $number := fn:count($book)
order by $number descending
return element {$name} {attribute value {$facet}, attribute number {$number} }
return
element facet {
attribute type {$name},
$facets
}
};
let $csv := fetch:text("https://gist.githubusercontent.com/CliffordAnderson/5be754415308dc407fc9/raw/daa23df71ceef375b83dd5b290284f83beeb710b/books.csv")
let $facets := fn:doc("https://gist.githubusercontent.com/CliffordAnderson/ed756303e2cbb63f5fc8/raw/ed20e129ff34767856b9f12047874f5c242630b4/facets.xml")
let $books := csv:parse($csv, map { 'header': true() })/csv/record
for $facet in $facets/terms/term
return local:build-facet($books, $facet/path/text(), $facet/facet/text() )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment