Skip to content

Instantly share code, notes, and snippets.

@telic
Last active January 23, 2017 21:36
Show Gist options
  • Save telic/d4a30a0ac94c0396d51e149364d2af14 to your computer and use it in GitHub Desktop.
Save telic/d4a30a0ac94c0396d51e149364d2af14 to your computer and use it in GitHub Desktop.
XQuery type name introspection
declare function local:type($item as item()?) as xs:string {
typeswitch ($item)
(: NOTE: eXist doesn't support XML Schema 1.1 types yet
case xs:dateTimeStamp return 'xs:dateTimeStamp'
:)
case xs:dateTime return 'xs:dateTime'
case xs:date return 'xs:date'
case xs:time return 'xs:time'
case xs:yearMonthDuration return 'xs:yearMonthDuration'
case xs:dayTimeDuration return 'xs:dayTimeDuration'
case xs:duration return 'xs:duration'
case xs:float return 'xs:float'
case xs:double return 'xs:double'
case xs:unsignedByte return 'xs:unsignedByte'
case xs:unsignedShort return 'xs:unsignedShort'
case xs:unsignedInt return 'xs:unsignedInt'
case xs:unsignedLong return 'xs:unsignedLong'
case xs:positiveInteger return 'xs:positiveInteger'
case xs:nonNegativeInteger return 'xs:nonNegativeInteger'
case xs:byte return 'xs:byte'
case xs:short return 'xs:short'
case xs:int return 'xs:int'
case xs:long return 'xs:long'
case xs:negativeInteger return 'xs:negativeInteger'
case xs:nonPositiveInteger return 'xs:nonPositiveInteger'
case xs:integer return 'xs:integer'
case xs:decimal return 'xs:decimal'
case xs:gYearMonth return 'xs:gYearMonth'
case xs:gYear return 'xs:gYear'
case xs:gMonthDay return 'xs:gMonthDay'
case xs:gDay return 'xs:gDay'
case xs:gMonth return 'xs:gMonth'
case xs:ENTITY return 'xs:ENTITY'
case xs:IDREF return 'xs:IDREF'
case xs:ID return 'xs:ID'
case xs:NCName return 'xs:NCName'
case xs:Name return 'xs:Name'
case xs:NMTOKEN return 'xs:NMTOKEN'
case xs:language return 'xs:language'
case xs:token return 'xs:token'
case xs:normalizedString return 'xs:normalizedString'
case xs:string return 'xs:string'
case xs:boolean return 'xs:boolean'
case xs:base64Binary return 'xs:base64Binary'
case xs:hexBinary return 'xs:hexBinary'
case xs:anyURI return 'xs:anyURI'
case xs:QName return 'xs:QName'
case map(*) return 'map'
(: NOTE: only available in xquery 3.1 :)
case array(*) return 'array'
case function(*) return 'function'
case document-node() return 'document-node'
case element() return 'element'
case attribute() return 'attribute'
case processing-instruction() return 'processing-instruction'
case comment() return 'comment'
case text() return 'text'
(: NOTE: eXist does not support namespace-node() test yet :)
case node() return 'namespace-node'
case empty-sequence() return 'empty-sequence'
default return 'unknown'
};
@telic
Copy link
Author

telic commented Jan 23, 2017

eXist also has a built-in function for this: util:get-sequence-type($sequence-type as xs:anyType*) as xs:string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment