Skip to content
Type System & Casting

Type System & Casting

goxpath implements the XSD type system with full subtype hierarchy, type-preserving arithmetic, and strict cast validation.

Numeric Type Hierarchy

xs:double  (IEEE 754 double)
xs:float   (IEEE 754 single, stored as double)
xs:decimal (arbitrary precision, no INF/NaN)
  └─ xs:integer
       ├─ xs:long → xs:int → xs:short → xs:byte
       ├─ xs:nonNegativeInteger
       │    ├─ xs:unsignedLong → xs:unsignedInt → xs:unsignedShort → xs:unsignedByte
       │    └─ xs:positiveInteger
       ├─ xs:nonPositiveInteger → xs:negativeInteger
       └─ (xs:integer itself)

Type Promotion in Arithmetic

Arithmetic operations promote types: integer + integer → integer, integer + decimal → decimal, anything + double → double.

1 + 2                   (: xs:integer — preserved :)
1 + 2.0                 (: xs:decimal — promoted :)
1 + 1.0e0               (: xs:double — promoted :)
1 div 2                 (: xs:decimal — div always produces at least decimal :)
7 idiv 2                (: xs:integer — integer division :)

Range Validation

Derived integer types enforce value ranges:

Type Range
xs:byte -128 to 127
xs:short -32768 to 32767
xs:int -2147483648 to 2147483647
xs:unsignedByte 0 to 255
xs:unsignedShort 0 to 65535
xs:positiveInteger 1 to max
xs:negativeInteger min to -1

String Type Hierarchy

xs:string
  └─ xs:normalizedString
       └─ xs:token
            ├─ xs:language (BCP 47 tags)
            ├─ xs:NMTOKEN
            └─ xs:Name → xs:NCName → xs:ID, xs:IDREF, xs:ENTITY

String subtypes enforce lexical constraints: xs:language validates BCP 47 format, xs:NCName validates XML NCName syntax, xs:token rejects leading/trailing whitespace.

Other Types

Type Description
xs:boolean true / false
xs:dateTime, xs:date, xs:time Date/time values
xs:duration, xs:dayTimeDuration, xs:yearMonthDuration Duration values
xs:gYear, xs:gMonth, xs:gDay, xs:gYearMonth, xs:gMonthDay Partial date values
xs:anyURI URI values
xs:untypedAtomic Untyped atomic values
xs:hexBinary, xs:base64Binary Binary data
xs:QName Qualified names

Cast Validation

Casting between types follows the XPath casting rules matrix. Invalid casts raise typed errors:

Error Meaning
XPTY0004 Incompatible source/target types (e.g., xs:double cast as xs:date)
FORG0001 Invalid value for target type (e.g., "abc" cast as xs:integer)
FOCA0002 Value out of range (e.g., INF cast as xs:integer)
XPST0080 Invalid cast target (e.g., cast as xs:NOTATION)

Cast Compatibility

Not all type combinations are allowed. Examples:

(: Allowed :)
xs:string("42") cast as xs:integer           (: 42 :)
xs:integer(42) cast as xs:double             (: 42.0 :)
xs:dateTime("2024-01-15T10:30:00") cast as xs:date  (: 2024-01-15 :)
true() cast as xs:integer                     (: 1 :)

(: Not allowed — XPTY0004 :)
xs:double(3.14) cast as xs:date              (: error :)
xs:boolean(true()) cast as xs:gYear          (: error :)

Instance Of

instance of checks follow the full subtype hierarchy:

xs:long(5) instance of xs:integer      (: true — long IS-A integer :)
xs:long(5) instance of xs:short        (: false — long is NOT short :)
xs:integer(5) instance of xs:decimal   (: true — integer IS-A decimal :)
xs:NCName("foo") instance of xs:token  (: true — NCName IS-A token :)
xs:token("foo") instance of xs:NCName  (: false — token is NOT NCName :)

Supported type tests: item(), node(), element(), attribute(), text(), map(*), array(*), function(*), and all atomic types.