-
-
Save EdWarga/20fd6e2ea8f4235dd68f to your computer and use it in GitHub Desktop.
Facets with Eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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