FHIRPath Reference¶
Complete reference for all FHIRPath functions and operators supported in FHIRKit.
Quick Reference¶
| Category | Functions |
|---|---|
| Existence | exists, empty, count, all, allTrue, anyTrue, allFalse, anyFalse, hasValue |
| Subsetting | first, last, tail, take, skip, single |
| Filtering | where, select, repeat, ofType |
| Navigation | children, descendants |
| String | startsWith, endsWith, contains, matches, replace, replaceMatches, length, substring, upper, lower, trim, split, join, indexOf, toChars, encode, decode |
| Math | abs, ceiling, floor, round, truncate, sqrt, ln, log, power, exp |
| Date/Time | today, now, timeOfDay |
| Type Conversion | toBoolean, toInteger, toDecimal, toString, toDate, toDateTime, toTime, toQuantity |
| Type Testing | convertsToBoolean, convertsToInteger, convertsToDecimal, convertsToString, convertsToDate, convertsToDateTime, convertsToTime, convertsToQuantity |
| Boolean Logic | not, iif, trace |
| Comparison | =, !=, ~, !~, <, >, <=, >= |
| Collections | distinct, isDistinct, union, intersect, exclude, combine, flatten, subsetOf, supersetOf |
| FHIR-Specific | resolve, extension, conformsTo, memberOf, subsumes, subsumedBy |
Existence Functions¶
Functions for testing the existence and state of collections.
exists()¶
Returns true if the collection has any elements.
Examples:
Patient.name.exists() // true if patient has any names
Patient.name.where(use = 'official').exists() // true if has official name
Patient.telecom.exists(system = 'phone') // true if has phone contact
empty()¶
Returns true if the collection is empty.
Examples:
Patient.deceased.empty() // true if deceased element not present
{}.empty() // true
{1, 2, 3}.empty() // false
count()¶
Returns the number of elements in the collection.
Examples:
Patient.name.count() // number of names
Patient.telecom.count() // number of contact points
{1, 2, 3}.count() // 3
{}.count() // 0
all()¶
Returns true if all elements in the collection satisfy the criteria.
Examples:
Patient.telecom.all(system.exists()) // true if all contacts have system
{1, 2, 3}.all($this > 0) // true (all positive)
{1, -1, 2}.all($this > 0) // false
allTrue()¶
Returns true if all items in the collection are true.
Examples:
{true, true, true}.allTrue() // true
{true, false, true}.allTrue() // false
{}.allTrue() // true (vacuously true)
anyTrue()¶
Returns true if any item in the collection is true.
Examples:
{false, true, false}.anyTrue() // true
{false, false, false}.anyTrue() // false
{}.anyTrue() // false
allFalse()¶
Returns true if all items in the collection are false.
Examples:
{false, false, false}.allFalse() // true
{false, true, false}.allFalse() // false
{}.allFalse() // true (vacuously true)
anyFalse()¶
Returns true if any item in the collection is false.
Examples:
{true, false, true}.anyFalse() // true
{true, true, true}.anyFalse() // false
{}.anyFalse() // false
hasValue()¶
Returns true if the input collection has a single value that is not null.
Examples:
Patient.birthDate.hasValue() // true if birthDate exists and has value
(5).hasValue() // true
{}.hasValue() // false
Subsetting Functions¶
Functions for extracting subsets of collections.
first()¶
Returns the first element of the collection.
Examples:
last()¶
Returns the last element of the collection.
Examples:
tail()¶
Returns all but the first element of the collection.
Examples:
take(num)¶
Returns the first num elements of the collection.
Examples:
skip(num)¶
Returns all elements except the first num.
Examples:
single()¶
Returns the single element if collection has exactly one, otherwise empty.
Examples:
{5}.single() // 5
{1, 2}.single() // {} (error/empty - not single)
{}.single() // {} (empty)
Patient.birthDate.single() // the birthDate if present
Filtering Functions¶
Functions for filtering and transforming collections.
where(criteria)¶
Filters collection to elements matching the criteria.
Examples:
Patient.name.where(use = 'official')
Patient.telecom.where(system = 'phone')
{1, 2, 3, 4, 5}.where($this > 3) // {4, 5}
Observation.component.where(code.coding.code = '8480-6')
select(projection)¶
Projects each element through an expression, flattening the result.
Examples:
Patient.name.select(given) // all given names flattened
Patient.telecom.select(value) // all contact values
{1, 2, 3}.select($this * 2) // {2, 4, 6}
repeat(expression)¶
Repeatedly evaluates expression on each item until no new items are produced.
Examples:
// Navigate through all extensions recursively
Patient.repeat(extension)
// Get all descendants (alternative to descendants())
resource.repeat(children())
ofType(type)¶
Filters to elements of the specified type.
Examples:
Bundle.entry.resource.ofType(Patient) // all Patient resources
Bundle.entry.resource.ofType(Observation) // all Observations
Observation.value.ofType(Quantity) // value if it's a Quantity
Navigation Functions¶
Functions for navigating resource structure.
children()¶
Returns all immediate child nodes.
Examples:
Patient.children() // all immediate children of Patient
Patient.name.children() // all parts of all names
descendants()¶
Returns all descendant nodes recursively.
Examples:
Patient.descendants() // all nested elements
Bundle.descendants().ofType(Observation) // all Observations anywhere
String Functions¶
Functions for string manipulation.
startsWith(prefix)¶
Returns true if string starts with prefix.
Examples:
'Hello World'.startsWith('Hello') // true
'Hello World'.startsWith('World') // false
Patient.name.family.startsWith('Sm') // true for 'Smith'
endsWith(suffix)¶
Returns true if string ends with suffix.
Examples:
contains(substring)¶
Returns true if string contains the substring.
Examples:
'Hello World'.contains('lo Wo') // true
Patient.name.text.contains('John') // true if name contains 'John'
matches(regex)¶
Returns true if string matches the regular expression.
Examples:
'test@email.com'.matches('.*@.*\\.com') // true
'12345'.matches('^\\d+$') // true (all digits)
Patient.telecom.value.matches('^\\+?\\d{10,}$') // phone pattern
replace(pattern, replacement)¶
Replaces first occurrence of pattern with replacement.
Examples:
'Hello World'.replace('World', 'FHIRPath') // 'Hello FHIRPath'
'foo-bar-baz'.replace('-', '_') // 'foo_bar-baz'
replaceMatches(regex, replacement)¶
Replaces all regex matches with replacement.
Examples:
'Hello123World456'.replaceMatches('\\d+', 'X') // 'HelloXWorldX'
'a-b-c'.replaceMatches('-', '_') // 'a_b_c'
length()¶
Returns the length of the string.
Examples:
substring(start, length?)¶
Returns substring starting at index with optional length.
Examples:
'Hello World'.substring(0, 5) // 'Hello'
'Hello World'.substring(6) // 'World'
'Hello'.substring(1, 3) // 'ell'
upper()¶
Converts string to uppercase.
Examples:
lower()¶
Converts string to lowercase.
Examples:
trim()¶
Removes leading and trailing whitespace.
Examples:
split(separator)¶
Splits string into a collection.
Examples:
join(separator?)¶
Joins collection of strings with separator.
Examples:
{'a', 'b', 'c'}.join(',') // 'a,b,c'
{'a', 'b', 'c'}.join() // 'abc'
Patient.name.given.join(' ') // 'John Michael'
indexOf(substring)¶
Returns index of first occurrence of substring (-1 if not found).
Examples:
toChars()¶
Converts string to collection of characters.
Examples:
encode(encoding) / decode(encoding)¶
Encodes/decodes string using specified encoding (base64, urlbase64, hex).
Examples:
Math Functions¶
Mathematical functions.
abs()¶
Returns absolute value.
Examples:
ceiling()¶
Returns smallest integer >= value.
Examples:
floor()¶
Returns largest integer <= value.
Examples:
round(precision?)¶
Rounds to specified precision (default 0).
Examples:
truncate()¶
Truncates decimal portion.
Examples:
sqrt()¶
Returns square root.
Examples:
ln()¶
Returns natural logarithm.
Examples:
log(base)¶
Returns logarithm to specified base.
Examples:
power(exponent)¶
Returns number raised to exponent.
Examples:
exp()¶
Returns e raised to the power of the number.
Examples:
Date/Time Functions¶
Functions for date and time operations.
today()¶
Returns the current date.
Examples:
now()¶
Returns the current date and time with timezone.
Examples:
timeOfDay()¶
Returns the current time.
Examples:
Type Conversion Functions¶
Functions for converting between types.
toBoolean()¶
Converts to boolean.
Conversion rules:
- true, 'true', 't', 'yes', 'y', '1', 1, 1.0 -> true
- false, 'false', 'f', 'no', 'n', '0', 0, 0.0 -> false
Examples:
toInteger()¶
Converts to integer.
Examples:
toDecimal()¶
Converts to decimal.
Examples:
toString()¶
Converts to string.
Examples:
toDate()¶
Converts to date.
Examples:
toDateTime()¶
Converts to datetime.
Examples:
'2024-12-16'.toDateTime() // @2024-12-16
'2024-12-16T10:30:00Z'.toDateTime() // @2024-12-16T10:30:00Z
toTime()¶
Converts to time.
Examples:
toQuantity(unit?)¶
Converts to quantity.
Examples:
Type Testing Functions¶
Functions for testing if conversion is possible.
convertsToBoolean()¶
Returns true if value can be converted to boolean.
convertsToInteger()¶
Returns true if value can be converted to integer.
convertsToDecimal()¶
Returns true if value can be converted to decimal.
convertsToString()¶
Returns true if value can be converted to string.
convertsToDate()¶
Returns true if value can be converted to date.
convertsToDateTime()¶
Returns true if value can be converted to datetime.
convertsToTime()¶
Returns true if value can be converted to time.
convertsToQuantity()¶
Returns true if value can be converted to quantity.
Boolean Logic Functions¶
not()¶
Returns boolean negation.
Examples:
iif(true-result, otherwise-result?)¶
If-then-else conditional.
Examples:
(5 > 3).iif('yes', 'no') // 'yes'
Patient.active.iif('Active', 'Inactive')
Patient.deceased.exists().iif('Deceased', 'Alive')
trace(name, projection?)¶
Logs collection for debugging, returns unchanged.
Examples:
Comparison Operators¶
Equality (=, !=)¶
Test equality/inequality.
Examples:
Equivalence (~, !~)¶
Test equivalence (case-insensitive for strings, empty = empty).
Examples:
Comparison (<, >, <=, >=)¶
Compare ordered values.
Examples:
5 < 10 // true
'apple' < 'banana' // true (lexicographic)
@2024-01-01 < @2024-12-31 // true
Patient.birthDate < @1990-01-01 // born before 1990
Collection Functions¶
Functions for set operations on collections.
distinct()¶
Returns collection with duplicates removed.
Examples:
isDistinct()¶
Returns true if all elements are unique.
Examples:
union(other)¶
Returns union of two collections (with duplicates removed).
Examples:
intersect(other)¶
Returns intersection of two collections.
Examples:
exclude(other)¶
Returns elements not in other collection.
Examples:
combine(other)¶
Combines collections (preserves duplicates).
Examples:
flatten()¶
Flattens nested collections.
Examples:
subsetOf(other)¶
Returns true if all elements are in other collection.
Examples:
supersetOf(other)¶
Returns true if collection contains all elements of other.
Examples:
FHIR-Specific Functions¶
Functions specific to FHIR resources.
resolve()¶
Resolves FHIR References to their target resources.
Examples:
Observation.subject.resolve() // resolves to Patient resource
MedicationRequest.medication.resolve() // resolves to Medication
extension(url)¶
Returns extensions with the specified URL.
Examples:
Patient.extension('http://hl7.org/fhir/StructureDefinition/patient-birthPlace')
Observation.extension('http://example.org/ext/certainty')
conformsTo(profile)¶
Returns true if resource conforms to the specified profile.
Examples:
Patient.conformsTo('http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient')
Observation.conformsTo('http://hl7.org/fhir/StructureDefinition/vitalsigns')
memberOf(valueset)¶
Returns true if code is in the specified ValueSet.
Examples:
Observation.code.memberOf('http://hl7.org/fhir/ValueSet/observation-vitalsignresult')
Condition.code.memberOf('http://example.org/ValueSet/diabetes-codes')
subsumes(code) / subsumedBy(code)¶
Tests subsumption relationships between codes.
Examples:
htmlChecks()¶
Validates HTML content in Narrative.
Examples:
Operators¶
Arithmetic Operators¶
+ Addition / string concatenation
- Subtraction
* Multiplication
/ Division
mod Modulo (remainder)
div Integer division
Examples:
5 + 3 // 8
'Hello' + ' World' // 'Hello World'
10 - 3 // 7
4 * 5 // 20
10 / 3 // 3.333...
10 mod 3 // 1
10 div 3 // 3
Boolean Operators¶
Examples:
Membership Operators¶
Examples:
5 in {1, 2, 3, 4, 5} // true
{1, 2, 3} contains 2 // true
Patient.gender in {'male', 'female'} // true
Type Operators¶
Examples:
Observation.value is Quantity // true if valueQuantity
Observation.value as Quantity // casts to Quantity or empty
Special Variables¶
$this¶
Refers to current item in iteration.
$index¶
Index of current item (0-based).
$total¶
Running total in aggregate operations.
%resource¶
Root resource being evaluated.
%context¶
Evaluation context (same as %resource for simple evaluations).
Next Steps¶
- FHIRPath Tutorial - Step-by-step learning
- FHIRPath Guide - Conceptual overview
- FHIRPath API - Python API reference