|
| 1 | +xquery version "3.1"; |
| 2 | +(: |
| 3 | + : For LICENSE-Details please refer to the LICENSE file in the root directory of this repository. |
| 4 | + :) |
| 5 | + |
| 6 | +(: IMPORTS ================================================================= :) |
| 7 | + |
| 8 | +import module namespace eutil = "http://www.edirom.de/xquery/eutil" at "../xqm/eutil.xqm"; |
| 9 | + |
| 10 | +(: NAMESPACE DECLARATIONS ================================================== :) |
| 11 | + |
| 12 | +declare namespace mei = "http://www.music-encoding.org/ns/mei"; |
| 13 | +declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization"; |
| 14 | +declare namespace request = "http://exist-db.org/xquery/request"; |
| 15 | + |
| 16 | + |
| 17 | +(: OPTION DECLARATIONS ===================================================== :) |
| 18 | + |
| 19 | +declare option output:method "json"; |
| 20 | +declare option output:media-type "application/json"; |
| 21 | + |
| 22 | + |
| 23 | +(: QUERY BODY ============================================================== :) |
| 24 | + |
| 25 | +(: Query parameter :) |
| 26 | +let $uri := request:get-parameter('uri', '') |
| 27 | + |
| 28 | +(: Load MEI document :) |
| 29 | +let $docUri := |
| 30 | + if (contains($uri, '#')) then |
| 31 | + (substring-before($uri, '#')) |
| 32 | + else |
| 33 | + ($uri) |
| 34 | +let $doc := eutil:getDoc($docUri) |
| 35 | + |
| 36 | +(: Extract relevant information :) |
| 37 | +let $artist := $doc//mei:titleStmt/mei:respStmt/mei:persName[@role = 'artist'] |
| 38 | +let $album := $doc//mei:meiHead/mei:fileDesc/mei:sourceDesc/mei:source[1]/mei:titleStmt/mei:title[1]/text() |
| 39 | +let $albumCover := $doc//mei:graphic[@type = 'cover']/string(@target) |
| 40 | + |
| 41 | +(: Build records objects :) |
| 42 | +let $records := |
| 43 | + for $rec in $doc//mei:recording |
| 44 | + let $recSource := $doc//mei:source[@xml:id = substring-after($rec/@decls, '#')] |
| 45 | + let $recTitle := $recSource/mei:titleStmt/string-join(mei:title, '; ') |
| 46 | + let $avFile := $rec/mei:avFile[1]/string(@target) |
| 47 | + let $avType := $rec/mei:avFile[1]/string(@mimetype) |
| 48 | + return |
| 49 | + map { |
| 50 | + 'title': replace($recTitle, '"', '\\"'), |
| 51 | + 'composer': replace($artist, '"', '\\"'), |
| 52 | + "work": replace($album, '"', '\\"'), |
| 53 | + "src": $avFile, |
| 54 | + (: "cover": $albumCover, :) |
| 55 | + "type": $avType |
| 56 | + } |
| 57 | + |
| 58 | +(: Build and return result object :) |
| 59 | +let $result := |
| 60 | + map { |
| 61 | + 'audios': array {$records}, |
| 62 | + 'success': true(), |
| 63 | + 'total': count($records) |
| 64 | + } |
| 65 | + |
| 66 | +return |
| 67 | + $result |
0 commit comments