To extract metadata from incoming messages, you may need to use a reference to another resource contained within the source resource. You can use the resolve() function to locate the target of a reference and extract a value from that other resource's metadata. The resolve() function can also be used to allow a fhirpath expression to query another resource that may have a reference to a patient name. If so, the reference is actually a pointer to the related Patient resource. To get to the name, you can resolve the Patient, and then continue to query against that resolved resource.
In the following example, the sample DocumentReference resource contains a Patient resource, which includes metadata for the patient's name that can be extracted. The DocumentReference also includes a reference in the subject element with a value of #localPatient1, which points to the id value of the Patient resource.
FHIRpath expressions that yield multiple results will not attempt to populate a value into the keyset. For example, an attempt to expand Patient.name.given fails if the resource contains two given names. Configure the FHIRpath expressions to always return a single value. For example, "Patient.name.given.first()" rather than "Patient.name.given".
"resourceType": "DocumentReference",
The following FHIRPath expression uses the reference in the subject element to locate the family name of the patient who is the subject of this document:
subject.resolve().name.family
The expression starts with the reference in the subject element and uses the resolve() function to follow the reference to the contained Patient resource that matches the value of the reference (#localPatient1). The rest of the expression locates the name element and family parameter within the Patient resource from which to extract the family name value, Smith.