(: explore if the proposed XQuery 4 function fn:type can be used to inspect sequences of items to find the most specific shared type among them :) declare namespace ist="//line-o.de/ns/inspect-sequence-types"; declare function ist:find-shared-type ($acc as xs:string*, $next as xs:string*) as xs:string* { let $count-a := count($acc) let $count-n := count($next) return if (head($acc) eq head($next)) then $acc else if ($count-a eq 0 or $count-n eq 0) then () else if ($count-a eq $count-n) then ist:find-shared-type(tail($acc), tail($next)) else if ($count-n gt $count-a) then ist:find-shared-type($acc, subsequence($next, $count-n - $count-a + 1, $count-a)) else ist:find-shared-type(subsequence($acc, $count-a - $count-n + 1, $count-n), $next) }; declare function ist:inspect-sequence-type($type-sequences as array(xs:string*)) as array(xs:string*) { array { array:fold-left($type-sequences, $type-sequences?1, ist:find-shared-type#2) } }; (: test data that assumes the output of fn:type(item()?) as xs:string* as outlined here https://github.com/qt4cg/qtspecs/issues/1550#issuecomment-2458192070 :) let $numeric := [ ('xs:integer', 'xs:decimal', 'xs:anyAtomicType', 'item()'), ('xs:positiveInteger', 'xs:integer', 'xs:decimal', 'xs:anyAtomicType', 'item()'), ('xs:decimal', 'xs:anyAtomicType', 'item()') ] let $atomic := array:append($numeric, ('xs:string', 'xs:anyAtomicType', 'item()')) let $mixed := array:append($atomic, ('element()', 'item()')) return ( ist:inspect-sequence-type($numeric), (: ["xs:decimal","xs:anyAtomicType","item()"] :) ist:inspect-sequence-type($atomic), (: ["xs:anyAtomicType","item()"] :) ist:inspect-sequence-type($mixed) (: ["item()"] :) )