xquery version "3.1"; import module namespace functx = 'http://www.functx.com'; declare namespace array = "http://www.w3.org/2005/xpath-functions/array"; declare namespace tei = "http://www.tei-c.org/ns/1.0"; declare namespace json = "http://basex.org/modules/json"; declare namespace map = "http://www.w3.org/2005/xpath-functions/map"; declare function local:search($jsonString) { let $map := json:parse($jsonString, map { 'format': 'xquery' }) let $fuzzy := xs:boolean($map("key1")) let $number := if ( (map:contains($map, "key2")) and (map:contains($map("key2"), "key3")) ) then $map("key2")("key3") else ("1", "2") return collection('ZK')/tei:TEI[ let $zettelType := tokenize( ./tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:msContents/@xml:id, "_" )[2] return ( (substring(data(./tei:text/tei:body/tei:div/tei:pb/@facs), 16, 1) = "V") or ($zettelType = "T") ) ][ if ($fuzzy) then (./tei:text/tei:body/tei:div[ descendant::text() contains text {"system"} using fuzzy ]) else (./tei:text/tei:body/tei:div[ descendant::text() contains text {"system"} ]) ][ functx:is-value-in-sequence( data( ./tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:msIdentifier/tei:altIdentifier[@type="logical_position"]/tei:idno[@type="logical_position"]/tei:idno[@type="zettelkasten"] ), $number ) ] }; local:search("{}")