# Using FHIRPath [LMA](/content/docs/hdp_product/maturity_model.html)

Realtime Export uses FHIRPath as its evaluation engine, which is likely to be unfamiliar to many people. While the [official documentation](http://hl7.org/fhirpath/) is excellent, this page will serve as a set of examples that can be used in Realtime Export

For all of the following examples, we will be applying the FHIRPath expressions to this example Patient.

```json
{
   "resourceType":"Patient",
   "id":"123",
   "meta":{
      "versionId":"1",
      "lastUpdated":"2020-09-15T18:38:07.182+00:00"
   },
   "identifier":[\
      {\
         "system": "http://example.org/mrn",\
         "value": "123"\
      },\
      {\
         "system": "http://example.org/person-id",\
         "value": "456"\
      }\
   ],
   "gender":"male",
   "name":[\
      {\
         "family":"Scott",\
         "given":[\
            "Michael",\
            "Gary"\
         ]\
      },\
      {\
         "family":"Levinson",\
         "given":[\
            "Astrid"\
         ]\
      }\
   ],
   "address":[\
      {\
         "use":"home",\
         "line":[\
            "#4 Privet Drive",\
            "Under the staircase"\
         ],\
         "city":"Little Whinging",\
         "district":"Surrey",\
         "postalCode":"ABC 123"\
      }\
   ]
}
```

Copy

## Basics

The first thing to know is that all FHIRPath expressions always return collections of elements. For example, this expression:

```none
Patient.name.given
```

Copy

Will return

```python
["Michael", "Gary", "Astrid"]
```

Copy

This makes intuitive sense, as there are 3 given name elements. However, even if there were a single element, it would return it wrapped in a collection like this:

```python
["Michael"]
```

Copy

For Realtime Export, the FHIRPath expressions must resolve to a single element. Refer to Collection Subsetting below for methods to mitigate this limitation.

# Collection Subsetting [LMA](/content/docs/hdp_product/maturity_model.html)

first() method

```none
Patient.name.given.first()
> ["Michael"]
```

Copy

last() method

```none
Patient.name.given.last()
> ["Astrid"]
```

Copy

Index slicing

```none
Patient.name.given[0]
> ["Michael"]
```

Copy

```none
Patient.name.given[1]
> ["Gary"]
```

Copy

There are more methods which can handle subsetting, which are [documented here](http://hl7.org/fhirpath/#subsetting).

# Self-referral [LMA](/content/docs/hdp_product/maturity_model.html)

Occasionally you will need to refer to the full content of a previous given FHIRPath expression. For occasions like this, you can use the `$this` expression, which will cause the element to evaluate to itself. Consider the following snippet, which has a child transformer on each line of an address.

```json
{
   "childTables": [\
      {\
         "fhirPath": "line",\
         "tableName": "rte_patient_address_line",\
         "childTransformer": {\
            "columns": [\
               {\
                  "columnName": "line",\
                  "fhirPath": "$this",\
                  "columnType": "STRING"\
               }\
            ]\
         }\
      }\
   ]
}
```

Copy

# Conditionals [LMA](/content/docs/hdp_product/maturity_model.html)

In FHIR, often times you will be faced with a high cardinality element, but what do you do when you need a specific element in the collection,
which you cannot safely index by its position in the list? In this case, FHIRPath supports use of the `.where()` function, which allows you to conditionally select from a collection:

```none
Patient.identifier.where(system='http://example.org/mrn').value > ["123"]
```

Copy

[Realtime Export Rules Definition](/content/docs/realtime_export/realtime_export_rules_definition.html)

Using FHIRPath

Realtime Export

[Realtime Export Overview](/content/docs/realtime_export/realtime_export_overview.html)
[Bootstrapping RTE Data](/content/docs/realtime_export/bootstrap_loading_rte.html)
[Realtime Export Rules Definition](/content/docs/realtime_export/realtime_export_rules_definition.html)
[Using FHIRPath](/content/docs/realtime_export/using_fhirpath.html)
[Delta Lake Export --EAP--](/content/docs/realtime_export/delta_lake_export.html)
[Bulk Batch Replication --EAP--](/content/docs/realtime_export/bulk_batch_replication.html)

[Delta Lake Export --EAP--](/content/docs/realtime_export/delta_lake_export.html)
