FHIR CI-Build

This is the Continuous Integration Build of FHIR (will be incorrect/inconsistent at times).
See the Directory of published versions icon

Note to Balloters: Key changes made since FHIR 6.0.0-ballot5:

  • Non-compatible Changes
    • (none)
  • Compatible, Substantive Changes
    • FHIR-54174 icon: FHIR should refer to the latest version of the FHIRPath specification
    • FHIR-53947 icon: FHIRPath Intro section(s) general improvements
    • FHIR-53959 icon: Additional Functions cleanup
    • FHIR-53948 icon: Clarification in Using FHIR Types in Expressions
    • FHIR-53950 icon: Use of FHIR Quantity - clarification on units systems and internal representation
    • FHIR-53952 icon: FHIR specific variables - clarifications and missing documentation on %profile
  • Non-substantive Changes
    • FHIR-54601 icon: Handling of difference in range of FHIR.decimal and System.Decimal
    • FHIR-53811 icon: Clarify hasValue() and getValue()
    • FHIR-40271 icon: FHIR is() example use non-singleton examples
    • FHIR-53962 icon: slice() should this only be available in invariant definitions
    • FHIR-53951 icon: all functions/features should have examples, return and argument types/cardinality should be explicit
    • FHIR-57259 icon: Clarify "complex types" in discussion of equality for structured values
    • FHIR-58514 icon: Outdated links to resources with other FHIRPath variables/functions
    • FHIR-53956 icon: FHIRPath page heading levels refined

Responsible Owner: FHIR Infrastructure icon Work GroupStandards Status: Normative

The FHIR Specification uses FHIRPath (release 3) icon for path-based navigation and extraction. FHIRPath is a separate specification published at http://hl7.org/fhirpath icon in order to support wider re-use across multiple specifications.

FHIRPath is used in several places in the FHIR and related specifications:

FHIRPath is used and extended in many other contexts beyond those listed above. For additional examples and information about other usage patterns and extensions, refer to the FHIRPath Usage and Extensions page icon on HL7's confluence.

FHIR inter-version compatibility rules allow for non-repeating elements to be made repeating in future FHIR versions. For this reason, FHIRPath expressions SHOULD be written presuming all elements will repeat, even if the current version of the model has a maximum cardinality of '1'.

 

In FHIRPath, like XPath, operations are expressed in terms of the logical content of hierarchical data models, and support traversal, selection and filtering of data.

FHIRPath uses a tree model that abstracts away the actual underlying data model of the data being queried. For FHIR, this means that the contents of the resources and data types as described in the Logical views (or the UML diagrams) are used as the model, rather than the JSON and XML formats, so specific xml or json features are not visible to the FHIRPath language (such as comments and the split representation of primitives).

More specifically:

  • A FHIRPath may optionally start with a full resource name (the type of the object)
  • Elements of datatypes and resources are used as the name of the nodes which can be navigated over, except for choice elements (ending with '[x]'), see below.
  • The json 'resourceType' property is not present in the object model - that is the type of the object (can restrict to specific types using ofType(Patient))
  • Elements of FHIR type Resource do not have the name of the resource as their first and only child, instead they directly contain the resource's children (as found in DomainResource.contained, Parameters.parameter.resource, Bundle.entry.resource, Bundle.issues, Bundle.entry.response.outcome)
  • There is no difference between an attribute and an element in XML (url and id are just considered elements in the object model)
  • Primitive FHIR types do not have a value element, but they do have id and extension elements
  • Repeating elements turn into multiple nodes with the same name

For choice elements, where elements can be one of multiple types, e.g., Patient.deceased[x]. In actual instances these will be present as either Patient.deceasedBoolean or Patient.deceasedDateTime. In FHIRPath, choice elements are labeled according to the name without the '[x]' suffix, and children can be explicitly treated as a specific type using the ofType function:

(Observation.value.ofType(Quantity)).unit

FHIRPath statements can start with a full resource name:

Patient.name.given

The name can also include super types such as DomainResource:

DomainResource.contained.where(id = 23).exists()

These statements apply to any resource that specializes DomainResource.

The namespace for the types defined in FHIR (primitive datatypes, datatypes, resources) is FHIR. So, for example:

Bundle.entry.resource.ofType(FHIR.Patient)

The first element - the type name (Bundle) - is not namespaced, but the parameter to the ofType() function is.

Understanding the primitive types is critical: FHIR.string is a different type to System.String. The FHIR.string type specializes FHIR.Element, and has the properties id, extension, and also an internal value of type System.String which is the FHIRPath primitive value. This primitive value can be explicitly accessed using the getValue() function defined below (e.g., Patient.birthDate.getValue()). However just referring to a FHIR primitive property will implicitly use the internal value (FHIRPath primitive type) if a function or operator is defined as requiring a specific FHIRPath primitive type.

The evaluation engine will automatically convert the value of FHIR types representing primitives to FHIRPath types when the function or operator requires a system primitive type. This implicit conversion will use the following mapping:

FHIR primitive type FHIRPath type
FHIR.boolean System.Boolean
FHIR.string, FHIR.uri, FHIR.code, FHIR.oid, FHIR.id, FHIR.uuid, FHIR.markdown, FHIR.base64Binary System.String
FHIR.integer, FHIR.unsignedInt, FHIR.positiveInt System.Integer
FHIR.integer64 System.Long
FHIR.decimal System.Decimal
FHIR.dateTime, FHIR.instant System.DateTime
FHIR.date System.Date
FHIR.time System.Time
FHIR.Quantity System.Quantity (see below)

Note that not all FHIR types have the same level of precision/range as their FHIRPath equivalent types.

  • The range of decimal in FHIR is 1e18 and in FHIRPath is 1e28,
  • FHIRPath datetimes can contain partial times, whereas fhir datetimes can be partial dates, or include a complete date and time (including timezone).

Some use cases may need to craft expressions carefully to handle these differences.

Since FHIR primitives may contain extensions, the following expressions are not mutually exclusive:

Patient.name.given = 'Ewout'                          // value of Patient.name.given as a string
Patient.name.given.extension.first().value = true     // extension of the primitive value
!Patient.active.hasValue() and !Patient.active.empty() // can both be true, if there are extensions but no value for Patient.active

The automatic conversion means that in most respects, a FHIR primitive can generally be treated as if it was the equivalent FHIRPath system type. The primary exception is the is() function, where the difference is explicit:

Patient.name.given.is(FHIR.string)
Patient.name.given.is(System.String).not()
Patient.name.given.getValue().is(System.String)

This also applies to e.g. Resource.id:

Patient.id is System.String
(Patient.id is FHIR.id).not

As shown, all FHIR primitives have the function getValue() defined (see below) for the few edge cases where the automatic conversion is not appropriate. Note that ofType() does not have such restrictions - both of the following are valid:

Patient.name.given.ofType(FHIR.string)   // returns all the given names (as they are fhir strings)
Patient.name.given.ofType(System.String) // {} ; returns the empty set, as these are not System.String values

The Mapping from FHIR.Quantity to FHIRPath System.Quantity can only be applied if the FHIR Quantity has a valid code present and also has either the UCUM system (http://unitsofmeasure.org), or the FHIRPath calendar system (http://hl7.org/fhirpath/CodeSystem/calendar-units).

When performing date arithmetic or other places where conversion is mandated by context, time-valued UCUM units are mapped to the approximate or equivalent relationships shown in the calendar duration units table icon in the base FHIRPath specification

i.e., The FHIR Quantity 1 'a' would be implicitly converted to the FHIRPath System.Quantity 1 'year'. Note that there's a subtle difference between the UCUM definitions for a and mo, which are definition durations of 365.25 and 30 days respectively, while year and month are calendar based durations, and their length of time varies. See Time-valued Quantities icon for further discussion. Implementers should be aware of these subtle differences, but in general, this approach matches what users expect most closely.

 

If an expression attempts to perform math requiring unit conversion on a Quantity with a code and system combination not supported by the engine (i.e. not one of the two systems above or any other codes the engine has been customized to support), the operation will return an empty collection.

For more details on using functions and operations with quantities, refer to the following sections: DateTime Arithmetic icon, toQuantity() icon, and Quantity Equality icon

FHIR defines several specific variables that are always in scope when FHIRPath is used in any of the contexts above:

Variable Description
%context The entry point for the evaluation of the FHIRPath expression (can be any element in a resource depending on usage context)
%resource The resource that contains the original node that is in %context
%rootResource The container resource for the resource identified by %resource

The resource is very often the context, such that %resource = %context. When a DomainResource contains another resource, and that contained resource is the focus (%resource) then %rootResource refers to the container resource. Note that in most cases, the resource is not contained by another resource, and then %rootResource is the same as %resource.

For example, a profile could define an invariant on Patient.identifier to ensure usage of a particular system is unique:

system = 'http://example.org/unique-identifier' implies %resource.identifier.where(system.getValue() = %context.system).count() = 1

Inside the invariant we can refer to %resource to access all identifiers, and then use %context inside the where clause to compare the iterated identifier's system with the system of the specific identifier being validated.

In addition to the general purpose variables above, additional variables and/or guidance about variable use are defined when working with specific resources. Refer to the following for additional FHIRPath guidance defined for:

Note: Other implementation guides can define additional variables and guidance for use in their specific context.

FHIR adds (compatible) functionality to the set of common FHIRPath functions.

Will filter the input collection for items named "extension" with the given url. This is a syntactical shortcut for .extension.where(url = string), but is simpler to write. If the input collection is empty or the url is empty, the return value will be an empty collection.

For example, to read the mother's maiden name extension:

Patient.extension('http://hl7.org/fhir/StructureDefinition/patient-mothersMaidenName').value

If the input collection contains a single value which is a FHIR primitive, this function will return true if that primitive has a value (as opposed to not having a value and just having extensions) and false if not. If the input collection isn't a single FHIR primitive, the result is an empty collection.

Note to implementers: The FHIR conceptual model talks about "primitives" as subclasses of the type Element that also have id and extensions. What this actually means is that a FHIR primitive is not a primitive in an implementation language. The introduction (section 2 above) describes the navigation tree as if the FHIR model applies - primitives are both primitives and elements with children.

In FHIRPath, this means that FHIR primitives have a value child, but, as described above, they are automatically cast to FHIRPath primitives when comparisons are made, and that the primitive value will be included in the set returned by children() or descendants().

For example:

Patient.active.hasValue()
// true ; if active has a Boolean value, whether active is either true or false
// false ; if active is present with only id and/or extensions
// {} ; if active is absent

Return the underlying value from the FHIR primitive as a system primitive if the input collection contains a single value which is a FHIR primitive, and it has a primitive value (see discussion for hasValue()). Otherwise, the result is an empty collection.

For example:

Patient.birthDate.getValue()
// returns just the primitive, does not include extensions (such as the birthTime extension)

For each item in the collection, if it is a string that is a uri (or canonical or url), locate the target of the reference, and add it to the resulting collection. If the item does not resolve to a resource, the item is ignored and nothing is added to the output collection.

The items in the collection may also represent a Reference, in which case the Reference.reference is resolved. If the input is empty, the return value will be an empty collection.

Resources can be resolved from contained resources, bundles, or other locations as supported by the engine (such as a FHIR server).
Where the reference is via a canonical uri, the engine should resolve it to the appropriate resource, potentially considering the version if specified (e.g. http://hl7.org/fhir/StructureDefinition/Patient|4.0.1).
Where the reference is via a Reference, and the value is a versioned reference, the engine should resolve it to the appropriate version of the resource if available (e.g. Patient/pat1/_history/23).

Patient.managingOrganization.resolve().contact
// returns all the contact details for the organization

Observation.subject.where(resolve() is Patient)
// returns the reference to the subject if it resolves to a patient (commonly used in search parameters to index by a specific resource type - noting that it doesn't actually return any data from the resource resolved)

When implementing resolve(), it is at the discretion of the engine what protocols, FHIR formats and versions are supported and whether and which logical models are handled.

For example:

DocumentReference.content.attachment.url.resolve()
// When using DocumentReference as an index entry to resources stored elsewhere

Returns the FHIR element definition information for each element in the input collection. If the input collection is empty, the return value will be an empty collection.

For example:

Patient.identifier.first().elementDefinition()
// returns the element definition for the patient's identifier element

Returns the input collection filtered to items that conform to the specified profile slice.

The structure argument is the (possibly versioned) canonical uri of the structure definition that contains the specified slice.
The name argument is either the ElementDefinition.sliceName for the element slice (if the sliceName is unique in the snapshot), or the ElementDefinition.id of the element slice.

If the structure cannot be resolved, or the name of the slice is not present in the profile, or those parameters are empty, the return value will be an empty collection.

If the slice does not match any element in the input collection, or if the input collection is empty, the return value will be an empty collection.

When using slice in a profile constraint (invariant) the structure argument can use the special variable %profile which refers to the profile in which the ElementDefinition.constraint containing the FHIRPath is defined.

For example, this invariant could appear in a Patient profile:

// ensure that a patient has at least one home phone and one home email (based on the slicing defined in the profile)
Patient.telecom.slice(%profile, 'homephone').exists() and Patient.telecom.slice(%profile, 'homeemail').exists()
Note: not all FHIRPath engines are in a position to implement slice() as implementation requires implementation of a FHIR validation engine, which is not a small undertaking.

For each element in the input collection, verifies that there are no modifying extensions defined other than the ones given by the modifier argument (comma-separated string). If the check passes, the input collection is returned. Otherwise, an error is thrown, including if modifier is empty.

For example:

MedicationRequest.checkModifiers('http://example.org/fhir/StructureDefinition/anti-prescription')
// true ; If the resource doesn't have any modifier extensions (other than the ones specified)

Task.checkModifiers('http://hl7.org/fhir/StructureDefinition/request-doNotPerform,http://example.org/fhir/StructureDefinition/known-modifier')
// true ; If the resource doesn't have any other modifier extensions (comma separated)

Returns true if the single input element conforms to the profile specified by the structure argument, and false otherwise. If the input is not a single item, the structure is empty, or the structure cannot be resolved to a valid profile, the return value is empty.

For example:

Patient.conformsTo('http://example.org/fhir/StructureDefinition/Patient-local-strict')
// true ; If the resource conforms to this profile

When invoked on a single code-valued element, returns true if the code is a member of the given valueset. When invoked on a single concept-valued element, returns true if any code in the concept is a member of the given valueset. When invoked on a single string, returns true if the string is equal to a code in the valueset, so long as the valueset only contains one codesystem. If the valueset in this case contains more than one codesystem, the return value is empty.

If the valueset cannot be resolved as a uri to a value set, or the input is empty or has more than one value, the return value is empty.

Note that implementations are encouraged to make use of a terminology service to provide this functionality.

For example:

Observation.component.where(code.memberOf('http://hl7.org/fhir/ValueSet/observation-vitalsignresult'))

This expression returns components that have a code that is a member of the observation-vitalsignresult valueset.


When invoked on a Coding-valued element and the given code is Coding-valued, returns true if the source code is equivalent to the given code, or if the source code subsumes the given code (i.e., the source code is an ancestor of the given code in a subsumption hierarchy), and false otherwise.

If the Codings are from different code systems, the relationships between the code systems must be well-defined or the return value is empty.

When the source or given elements are CodeableConcepts, returns true if any Coding in the source or given elements is equivalent to or subsumes the given code.

If either the input or the code parameter are not single value collections, the return value is empty.

Note that implementations are encouraged to make use of a terminology service to provide this functionality.

For example:

Condition.code.subsumes(Coding { system: 'http://snomed.info/sct', code: '3738000' })
// true ; if the condition is equivalent or subsumes the SNOMED CT concept 3738000, Viral hepatitis
// $subsumes operation returned 'equivalent' or 'subsumes'

For more information, see Subsumption Testing in the FHIR terminology service documentation.


When invoked on a Coding-valued element and the given code is Coding-valued, returns true if the source code is equivalent to the given code, or if the source code is subsumed by the given code (i.e., the given code is an ancestor of the source code in a subsumption hierarchy), and false otherwise.

If the Codings are from different code systems, the relationships between the code systems must be well-defined or a run-time error is thrown.

When the source or given elements are CodeableConcepts, returns true if any Coding in the source or given elements is equivalent to or subsumed by the given code.

If either the input or the code parameter are not single value collections, the return value is empty.

Note that implementations are encouraged to make use of a terminology service to provide this functionality.

For example:

Condition.code.subsumedBy(Coding { system: 'http://snomed.info/sct', code: '235856003' })
// true ; if the condition is equivalent or subsumed by the SNOMED CT concept 235856003, Disorder of liver
// $subsumes operation returns 'subsumed-by' or 'equivalent'

For more information, see Subsumption Testing in the FHIR terminology service documentation.


When invoked on a single xhtml element, the function returns true if the rules around HTML usage are met, and false if they are not. If invoked on items of type 'string', the contents of the string are parsed as the content of a div and the function returns false if the string does not parse as valid HTML or fails the rules around HTML usage. The return value is empty on any other kind of element or if invoked on a collection of elements.

For example:

Patient.text.div.htmlChecks()
// true ; if the div element meets the rules around HTML usage

Questionnaire.item.extension('http://hl7.org/fhir/StructureDefinition/rendering-xhtml').valueString.htmlChecks()
// true ; also supported on string where required.

This functions returns the ordinal value for an element. This may be based on an itemWeight extension defined on an element such as QuestionnaireResponse answer, or it may be based on the weight property defined on a code. In some cases, this may require looking across resources. For example, QuestionnaireResponse.item.where(linkId='123').answer.first().weight() may need to find the corresponding item in the Questionnaire and then look up the coding specified in 'answer' with the corresponding answerOption in Questionnaire.item with the same linkId. If no weight is defined for the context element, the return value is empty. If the FHIRPath engine is unable to resolve the corresponding value set, code system or questionnaire options, it SHOULD cause the expression to fail.

This function will typically be used to support scoring of Questionnaires, but it can be used with observations or potentially other elements as well.

For example:

Questionnaire.item.where(linkId='123').item.sum(weight())
// sum of the weights for all items with the specified linkId

FHIR also includes overrides to some functions providing details on how the function will work with FHIR specific types.

The native FHIRPath toString() icon function works with FHIR datatypes by implicitly converting FHIR primitive elements to their corresponding FHIRPath system types before invoking the standard `toString()` operation. Any other types, or FHIR primitives that do not have an actual value (such as those with only extensions or an element id) will result in an empty collection.

I.e. this will only return a value where the input collection is a single value, hasValue() returns true, and the FHIR primitive has a corresponding FHIRPath system type.
For example, if a FHIR decimal element contained no value and only has a data absent reason extension, then the result would be an empty collection.

For example:

Patient.birthDate.toString()
// return the string representation of the birthDate if it has an actual value, otherwise an empty collection

Patient.contact.toString()
// returns an empty string as the contact element is not a FHIR primitive datatype

Returns a collection that contains all items in the input collection that are of the given type or a subclass thereof. This is as defined as in the base FHIRPath specification, but implementers should be aware that in FHIR, only concrete core types are allowed as an argument. All primitives are considered to be independent types (so canonical is not a subclass of string, but markdown is, as indicated on the datatypes page). Profiled types are not allowed, so to select SimpleQuantity one would pass Quantity as an argument.

For example:

Observation.value.ofType(CodeableConcept)
// filter the observation value to only include CodeableConcept instances

Observation.value.ofType(dateTime) | Observation.value.ofType(Period).start
// selects either dateTime or the start of a period value (both of type dateTime)

Observation.extension.value.ofType(string)
// filters the extension values to only include string instances
// or datatypes derived from string (such as valueMarkdown, valueCode or valueId)

QuestionnaireResponse.questionnaire.ofType(string)
// {} ; as this property is a canonical, this will return an empty collection

Equivalence works in exactly the same manner, but with the addition that for complex types (including resources), equivalence requires all child properties to be equivalent recursively, except for "id" elements.

In addition, for Coding values, equivalence is defined based on the code and system elements only. The version, display, and userSelected elements are ignored for the purposes of determining Coding equivalence.

For CodeableConcept values, equivalence is defined as a non-empty intersection of Coding elements, using equivalence. In other words, two CodeableConcepts are considered equivalent if any Coding in one is equivalent to any Coding in the other.

The FHIR specification adds support for additional environment variables:

The following environmental values are set for all contexts:

%sct        // (string) url for snomed ct
%loinc      // (string) url for loinc
%`vs-[name]` // (string) full url for the provided HL7 value set with id [name]
%`ext-[name]` // (string) full url for the provided HL7 extension with id [name]
%resource	// The original resource current context is part of. When evaluating a datatype, this would be the resource the element is part of. Do not go past a root resource into a bundle, if it is contained in a bundle.

// Note that the names of the `vs-` and `ext-` constants are quoted (just like paths) to allow "-" in the name.

For example:

Observation.component.where(code.memberOf(%`vs-observation-vitalsignresult`))

This expression returns components that have a code that is a member of the observation-vitalsignresult valueset.

Implementation Note: Implementation Guides are allowed to define their own externals, and implementers should provide some appropriate configuration framework to allow these constants to be provided to the evaluation engine at run-time. E.g.:

%`us-zip` = '[0-9]{5}(-[0-9]{4}){0,1}'

Authors of Implementation Guides should be aware that adding specific environment variables restricts the use of the FHIRPath to their particular context.

Note that these tokens are not restricted to simple types, and they may have fixed values that are not known before evaluation at run-time, though there is no way to define these kinds of values in implementation guides.

This page documents a restricted subset of the FHIRPath language icon that is used in a few contexts in this specification. When the restricted FHIRPath language subset is in use, the following rules apply:

  • All statements SHALL start with the name of the context element (e.g., on a Patient resource, Patient.contact.name.), or SHALL be simply "$this" to refer to the element that has focus
  • Operators SHALL NOT be used
  • Only the following functions may be used:
    • .resolve()
    • .extension("url")
    • .ofType(type)
    All other functions SHALL NOT be used

These rules exist to keep processing the path simple to support use of the path by processors that are not backed by a full FHIRPath implementation.

The following locations use this restricted FHIRPath language:

For example:

Patient.birthDate
Patient.name[0].family
Patient.name[2].given[1]
QuestionnaireResponse.item[0].item[2].answer[0].value