### FHIRPath Query Examples for HL7 V2 Messages Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Examples demonstrating how to query HL7 V2 messages using FHIRPath. These queries navigate segments, fields, and elements to extract specific data values. ```java Message.segment.where(code = 'PID').field[3].element.first().simple() ``` ```java Message.segment[2].elements(3).simple() ``` ```java Message.segment.where(code = 'PID').field[3].element.where(component[4].value = 'MR').simple() ``` -------------------------------- ### FHIRPath Aggregate Function Examples Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Provides examples of using the general-purpose `aggregate` function in FHIRPath to perform calculations like sum, minimum, and average. It explains the use of `$this`, `$index`, and `$total` iteration variables. ```java value.aggregate($this + $total, 0) // Sum value.aggregate(iif($total.empty(), $this, iif($this < $total, $this, $total))) // Min value.aggregate($total + $this, 0) / value.count() // Average ``` -------------------------------- ### FHIRPath Query Examples for HL7 V2 Messages Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Illustrates how to use FHIRPath to query data within HL7 Version 2 messages. These examples demonstrate accessing segments, fields, elements, and components. ```fhirpath Message.segment.where(code = 'PID').field[3].element.first().simple() Message.segment[2].elements(3).simple() Message.segment.where(code = 'PID').field[3].element.where(component[4].value = 'MR').simple() Message.segment.where(code = 'PID').elements(3).where(component[4].value = 'MR').component[1].text Message.group('PATIENT').group('PATIENT_OBSERVATION').item.ofType(Segment) .where(code = 'OBX' and elements(2).exists(components(2) = 'LN'))) ``` -------------------------------- ### FHIRPath Single-Line Comment Example Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates the syntax for single-line comments in FHIRPath, which start with two forward slashes and extend to the end of the line. Comments are ignored during evaluation. ```java 2 + 2 // This is a single-line comment ``` -------------------------------- ### Less or Equal (<=) Operator Examples in Java Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Provides examples of the less than or equal to operator (<=) in FHIRPath for various data types including numbers, strings, units, dates, times, and datetimes. It covers implicit type conversions and how precision affects the comparison, returning an empty set ({ }) when precisions do not match. ```java 10 <= 5 // true 10 <= 5.0 // true; note the 10 is converted to a decimal to perform the comparison 'abc' <= 'ABC' // true 4 'm' <= 4 'cm' // false (or { } if the implementation does not support unit conversion) @2018-03-01 <= @2018-01-01 // false @2018-03 <= @2018-03-01 // empty ({ }) @2018-03-01T10:30:00 <= @2018-03-01T10:00:00 // false @2018-03-01T10 <= @2018-03-01T10:30 // empty ({ }) @2018-03-01T10:30:00 <= @2018-03-01T10:30:00.0 // true @T10:30:00 <= @T10:00:00 // false @T10 <= @T10:30 // empty ({ }) @T10:30:00 <= @T10:30:00.0 // true ``` -------------------------------- ### Greater or Equal (>=) Operator Examples in Java Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates the greater than or equal to operator (>=) in FHIRPath with examples for numbers, strings, units, dates, times, and datetimes. It explains the impact of implicit type conversions and precision on comparison results, returning an empty set ({ }) for differing precisions. ```java 10 >= 5 // false 10 >= 5.0 // false; note the 10 is converted to a decimal to perform the comparison 'abc' >= 'ABC' // false 4 'm' >= 4 'cm' // true (or { } if the implementation does not support unit conversion) @2018-03-01 >= @2018-01-01 // true @2018-03 >= @2018-03-01 // empty ({ }) @2018-03-01T10:30:00 >= @2018-03-01T10:00:00 // true @2018-03-01T10 >= @2018-03-01T10:30 // empty ({ }) @2018-03-01T10:30:00 >= @2018-03-01T10:30:00.0 // true @T10:30:00 >= @T10:00:00 // true @T10 >= @T10:30 // empty ({ }) @T10:30:00 >= @T10:30:00.0 // true ``` -------------------------------- ### FHIRPath Quantity Comparison Examples Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Demonstrates the comparison of quantity values in FHIRPath, including valid and invalid unit comparisons, and the handling of time-valued quantities with calendar durations. ```fhirpath 1 year > 1 `a` // { } 10 seconds > 1 's' // true ``` -------------------------------- ### FHIRPath Less Than (<) Operator Examples Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Provides examples of the less than operator (<) in FHIRPath for various data types, including numbers, strings, quantities, dates, datetimes, and times. It highlights type compatibility, precision differences, and timezone considerations. ```fhirpath 10 < 5 // false 10 < 5.0 // false - note the 10 is converted to a decimal to perform the comparison 'abc' < 'ABC' // false 4 'm' < 4 'cm' // false (or { } if the implementation does not support unit conversion) @2018-03-01 < @2018-01-01 // false @2018-01-01 < @2018-01-01 // false - same precision @2018-03 < @2018-03-01 // empty ({ }) - different precisions @2018-03-01T10:30:00 < @2018-03-01T10:00:00 // false @2018-03-01T10 < @2018-03-01T10:30 // empty ({ }) - different precisions @2018-03-01T10:30:00 < @2018-03-01T10:30:00.0 // false - values are equal to seconds, trailing zeroes after the decimal are ignored @2018-01-01T16:00:00+11:00 < @2018-01-01T15:00:00.0+10:00 // false (same moment in diff timezones) @2018-01-01T16:00:00+12:00 < @2018-01-01T15:00:00.0+10:00 // true (4pm+12 is less than 5pm+10 when timezones are considered) @T10:30:00 < @T10:00:00 // false @T10 < @T10:30 // empty ({ }) - different precisions @T10:30:00 < @T10:30:00.0 // false - values are equal to seconds, trailing zeroes after the decimal are ignored ``` -------------------------------- ### FHIRPath Function Invocations Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Shows examples of function invocations in FHIRPath, including chaining functions for a fluent syntax and handling constants. ```fhirpath name.given.substring(0,4) identifier.where(use = 'official') (4+5).count() Patient.telecom.where(use = 'official').union(Patient.contact.telecom.where(use = 'official')).exists().not() ``` -------------------------------- ### Quantity Equivalence Examples (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates the equivalence comparison for quantities in FHIRPath, showing how units like 'year' and 'a' (annum) or 'second' and 's' are handled. It highlights that dimensions must match for equivalence. ```java 1 year ~ 1 'a' // true 1 second ~ 1 's' // true ``` -------------------------------- ### FHIRPath Equals Operator Examples Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Illustrates the usage and behavior of the FHIRPath Equals operator (=) with different data types and scenarios, including quantity and date/time comparisons. ```fhirpath Patient.telecom.where(system = 'phone') ``` ```fhirpath 1 year = 1 'a' // {} an empty collection 1 second = 1 's' // true ``` ```fhirpath @2012 = @2012 // returns true @2012 = @2013 // returns false @2012-01 = @2012 // returns empty ({ }) @2012-01-01T10:30 = @2012-01-01T10:30 // returns true @2012-01-01T10:30 = @2012-01-01T10:31 // returns false @2012-01-01T10:30:31 = @2012-01-01T10:30 // returns empty ({ }) @2012-01-01T10:30:31.0 = @2012-01-01T10:30:31 // returns true @2012-01-01T10:30:31.1 = @2012-01-01T10:30:31 // returns false ``` ```fhirpath @2017-11-05T01:30:00.0-04:00 > @2017-11-05T01:15:00.0-05:00 // false @2017-11-05T01:30:00.0-04:00 < @2017-11-05T01:15:00.0-05:00 // true @2017-11-05T01:30:00.0-04:00 = @2017-11-05T01:15:00.0-05:00 // false @2017-11-05T01:30:00.0-04:00 = @2017-11-05T00:30:00.0-05:00 // true ``` -------------------------------- ### BMI Calculation Example (FHIRPath) Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md An example demonstrating a FHIRPath expression to calculate Body Mass Index (BMI) using weight and height variables. This illustrates the use of mathematical functions like power and round within a practical context, such as in questionnaires. ```fhirpath (%weight/(%height.power(2))).round(1) // note that these variables are decimal values not quantities ``` -------------------------------- ### FHIRPath Less Than or Equal To (<=) Operator Examples Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Demonstrates the usage of the less than or equal to operator (<=) in FHIRPath. It returns true if the first operand is less than or equal to the second. Operands must be of the same type or implicitly convertible. Examples cover numeric, string, unit, date, time, and timezone comparisons. ```fhirpath 10 <= 5 // false 10 <= 5.0 // false - note the 10 is converted to a decimal to perform the comparison 'abc' <= 'ABC' // false 4 'm' <= 4 'cm' // false (or { } if the implementation does not support unit conversion) @2018-03-01 <= @2018-01-01 // false @2018-01-01 <= @2018-01-01 // true - equal with same precision @2018-03 <= @2018-03-01 // empty ({ }) - different precisions @2018-03-01T10:30:00 <= @2018-03-01T10:00:00 // false @2018-03-01T10 <= @2018-03-01T10:30 // empty ({ }) - different precisions @2018-03-01T10:30:00 <= @2018-03-01T10:30:00.0 // true - values are equal to seconds, trailing zeroes after the decimal are ignored @2018-01-01T16:00:00+11:00 <= @2018-01-01T15:00:00.0+10:00 // true (same moment in diff timezones) @2018-01-01T16:00:00+12:00 <= @2018-01-01T15:00:00.0+10:00 // true (4pm+12 is less than 5pm+10 when timezones are considered) @T10:30:00 <= @T10:00:00 // false @T10 <= @T10:30 // empty ({ }) - different precisions @T10:30:00 <= @T10:30:00.0 // true ``` -------------------------------- ### Get Type Information for Primitive Types (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Illustrates how to use the `type()` function in FHIRPath to get type information for primitive types like String. The result is a `SimpleTypeInfo` object containing the namespace, name, and base type. ```java ('John' | 'Mary').type() ``` ```json { SimpleTypeInfo { namespace: 'System', name: 'String', baseType: 'System.Any' }, SimpleTypeInfo { namespace: 'System', name: 'String', baseType: 'System.Any' } } ``` -------------------------------- ### Environment Variables in FHIRPath Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Provides examples of environment variables in FHIRPath, denoted by a '%' prefix. These variables are passed into the evaluation engine by the calling environment, allowing for external data and avoiding repetition of fixed values. ```java %ucum // (string) url for UCUM (http://unitsofmeasure.org, per http://hl7.org/fhir/ucum.html) %context // The original node that was passed to the evaluation engine before starting evaluation ``` -------------------------------- ### FHIRPath Operators for Expressions Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates the use of various operators in FHIRPath for mathematical operations, boolean logic, and collection manipulation. Includes examples for division, implication, union, and string concatenation. ```java Appointment.minutesDuration / 60 > 5 MedicationAdministration.wasNotGiven implies MedicationAdministration.reasonNotGiven.exists() name.given | name.family // union of given and family names 'sir ' + name.given ``` -------------------------------- ### FHIRPath Unary Negation and Parentheses Example Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Demonstrates the effect of operator precedence on unary negation in FHIRPath. Using parentheses ensures the unary negation is applied to the intended operand. ```fhirpath -7.combine(3) // ERROR (-7).combine(3) // { -7, 3 } ``` -------------------------------- ### FHIRPath Quantity Literals (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Provides examples of Quantity literals in FHIRPath, consisting of a number followed by a UCUM unit or calendar duration keyword in single quotes. ```java 4.5 'mg' 100 '[degF]' 1 year 4 days ``` -------------------------------- ### FHIRPath DateTime Literals (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Shows examples of DateTime literals in FHIRPath, combining Date and Time. Supports optional timezone offsets and partial date/time specifications. ```java @2014-01-25T14:30:14.559 @2014-01-25T14:30:14.559Z @2014-01-25T14:30 @2014-03-25T @2014-01T @2014T ``` -------------------------------- ### FHIRPath String Literals Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Demonstrates the syntax for string literals in FHIRPath, enclosed in single quotes. Includes examples of escape sequences for special characters and Unicode. ```fhirpath 'test string' 'urn:oid:3.4.5.6.7.8' ``` -------------------------------- ### FHIRPath Multi-Line Comment Example Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Illustrates the syntax for multi-line comments in FHIRPath, enclosed by `/*` and `*/`. Content within these delimiters is ignored by the FHIRPath engine. ```java /* This is a multi-line comment */ ``` -------------------------------- ### FHIRPath Existence Check Examples Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Illustrates the usage of the FHIRPath `exists()` function to check for the presence of elements within a collection. It shows basic checks and checks with criteria. ```java Patient.name.exists() Patient.identifier.exists(use = 'official') Patient.telecom.exists(system = 'phone' and use = 'mobile') Patient.generalPractitioner.exists($this is Practitioner) ``` -------------------------------- ### Quantity Comparison Examples (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates quantity comparison in FHIRPath, focusing on cases where units differ or are incomparable. It shows that time-valued quantities with different granularities (e.g., year vs. 'a') or exceeding second precision are considered un-comparable, resulting in an empty set. ```java 1 year > 1 `a` // { } (empty) 10 seconds > 1 's' // true ``` -------------------------------- ### Get distinct items in a collection using distinct() in Java Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc The `distinct()` operator returns a new collection containing only the unique items from the input collection, based on the equals operator. It returns an empty collection if the input is empty. This example retrieves the distinct tags on a Patient. ```java Patient.meta.tag.distinct() ``` -------------------------------- ### Date/Time Equivalence Examples (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Illustrates date and time equivalence in FHIRPath. It shows how precision differences affect the outcome, with different levels of precision resulting in `false` rather than empty. It also covers the handling of seconds and milliseconds. ```java @2012 ~ @2012 // returns true @2012 ~ @2013 // returns false @2012-01 ~ @2012 // returns false as well @2012-01-01T10:30 ~ @2012-01-01T10:30 // returns true @2012-01-01T10:30 ~ @2012-01-01T10:31 // returns false @2012-01-01T10:30:31 ~ @2012-01-01T10:30 // returns false as well @2012-01-01T10:30:31.0 ~ @2012-01-01T10:30:31 // returns true @2012-01-01T10:30:31.1 ~ @2012-01-01T10:30:31 // returns false ``` -------------------------------- ### substring Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Returns a portion of the string starting at a specified position. An optional length parameter can limit the number of characters returned. Handles various edge cases for start and length parameters. ```APIDOC ## substring(start : Integer [, length : Integer]) : String ### Description Returns the part of the string starting at position `start` (zero-based). If `length` is given, returns at most `length` characters. Returns empty if `start` is outside the string length. If `length` is empty, zero, or negative, specific behaviors apply. If the input collection contains multiple items, an error is signaled. ### Method N/A (FHIRPath function) ### Endpoint N/A (FHIRPath function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```fhirpath 'abcdefg'.substring(3) // 'defg' 'abcdefg'.substring(1, 2) // 'bc' 'abcdefg'.substring(6, 2) // 'g' 'abcdefg'.substring(7, 1) // { } (start position is outside the string) 'abcdefg'.substring(-1, 1) // { } (start position is outside the string) 'abcdefg'.substring(3, 0) // '' (empty string) 'abcdefg'.substring(3, -1) // '' (empty string) 'abcdefg'.substring(-1, -1) // {} (start position is outside the string) ``` ### Response #### Success Response (String) - **result** (String) - The extracted portion of the string. Returns empty if start is out of bounds or if input is empty. Returns an empty string if length is zero or negative. ``` -------------------------------- ### startsWith Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Checks if the input string begins with a specified prefix. Returns true if it does, false otherwise. Handles empty prefix and empty input string cases. ```APIDOC ## startsWith(prefix : String) : Boolean ### Description Returns `true` when the input string starts with the given `prefix`. If `prefix` is an empty string, the result is `true`. If the input collection is empty, the result is empty. If the input collection contains multiple items, an error is signaled. ### Method N/A (FHIRPath function) ### Endpoint N/A (FHIRPath function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```fhirpath 'abcdefg'.startsWith('abc') // true 'abcdefg'.startsWith('xyz') // false ``` ### Response #### Success Response (Boolean) - **result** (Boolean) - `true` if the string starts with the prefix, `false` otherwise. Returns `true` for an empty prefix. Returns empty if the input is empty. ``` -------------------------------- ### FHIRPath: Implicit Type Conversion Example Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md FHIRPath supports implicit type conversions when an operator or function is used with compatible types. For example, an Integer can be implicitly converted to a Decimal to perform decimal addition. ```fhirpath 5 + 10.0 // Integer 5 is implicitly converted to Decimal 5.0 for addition ``` -------------------------------- ### String Manipulation with Filters (FHIRPath) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc This example demonstrates how to apply string manipulation functions to collections with multiple items in FHIRPath by using `where()` and `select()` filters. This allows operations that normally require a single item to be applied iteratively. ```FHIRPath someCollection.where(item.isString()).select(item.someStringFunction()) ``` -------------------------------- ### FHIRPath Example for Anonymous Type Access Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Demonstrates how to access elements of an anonymous type in FHIRPath using the `Patient.contact` example. This snippet shows a common use case for navigating and extracting information from FHIR resources. ```fhirpath Patient.contact.single().type() ``` -------------------------------- ### substring: Extract part of a string in FHIRPath Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Returns a portion of the string starting at a specified position, optionally with a given length. Handles out-of-bounds start positions, empty inputs, and negative/zero lengths, and errors on multi-item collections. ```fhirpath 'abcdefg'.substring(3) // 'defg' 'abcdefg'.substring(1, 2) // 'bc' 'abcdefg'.substring(6, 2) // 'g' 'abcdefg'.substring(7, 1) // { } (start position is outside the string) 'abcdefg'.substring(-1, 1) // { } (start position is outside the string, // this can happen when the -1 was the result of a calculation rather than explicitly provided) 'abcdefg'.substring(3, 0) // '' (empty string) 'abcdefg'.substring(3, -1) // '' (empty string) 'abcdefg'.substring(-1, -1) // {} (start position is outside the string) ``` -------------------------------- ### Less Than (<) Operator Examples in Java Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Illustrates the less than operator (<) in FHIRPath across different data types such as numbers, strings, units, dates, times, and datetimes. It shows how implicit conversions and precision levels influence the comparison results, returning an empty set ({ }) for differing precisions. ```java 10 < 5 // false 10 < 5.0 // false; note the 10 is converted to a decimal to perform the comparison 'abc' < 'ABC' // false 4 'm' < 4 'cm' // false (or { } if the implementation does not support unit conversion) @2018-03-01 < @2018-01-01 // false @2018-03 < @2018-03-01 // empty ({ }) @2018-03-01T10:30:00 < @2018-03-01T10:00:00 // false @2018-03-01T10 < @2018-03-01T10:30 // empty ({ }) @2018-03-01T10:30:00 < @2018-03-01T10:30:00.0 // false @T10:30:00 < @T10:00:00 // false @T10 < @T10:30 // empty ({ }) @T10:30:00 < @T10:30:00.0 // false ``` -------------------------------- ### Ruby Flavor Sorting Examples Source: https://github.com/hl7/fhirpath/wiki/Sort-Syntax-Proposal-Discussion Provides examples of sorting arrays in Ruby using custom ascending and descending comparator blocks. It shows how to define and apply these blocks for sorting numerical data. ```Ruby ascending_sort = ->(a,b) { a <=> b } descending_sort = ->(a,b) { b <=> a } [1, 3, 2, 4].sort( & ascending_sort ) # => [1, 2, 3, 4] [1, 3, 2, 4].sort( & descending_sort ) # => [4, 3, 2, 1] foo = ascending_sort [1, 3, 2, 4].sort( & foo ) # => [1, 2, 3, 4] ``` -------------------------------- ### FHIRPath Path Navigation Examples Source: https://context7.com/hl7/fhirpath/llms.txt Demonstrates how to navigate hierarchical data structures using FHIRPath's dot-notation. It covers basic traversal, type-qualified paths, accessing repeating elements, indexer access, and chained navigation through complex structures. ```fhirpath // Basic path navigation - returns all given names across all patient names Patient.name.given // Type-qualified path - only processes if root is Patient type Patient.name.given // Access repeating elements - name can repeat, given can repeat within name Patient.name.given // Returns: { 'Wouter', 'Gert', 'John' } // Indexer access (0-based) - get first name element Patient.name[0] // Chained navigation through complex structures Bundle.entry.resource.ofType(Patient).telecom.where(system = 'phone') ``` -------------------------------- ### FHIRPath Greater Than or Equal To (>=) Operator Examples Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md Illustrates the use of the greater than or equal to operator (>=) in FHIRPath. It returns true if the first operand is greater than or equal to the second. Operands must be of the same type or implicitly convertible. Examples cover numeric, string, unit, date, and time comparisons. ```fhirpath 10 >= 5 // true 10 >= 5.0 // true - note the 10 is converted to a decimal to perform the comparison 'abc' >= 'ABC' // true 4 'm' >= 4 'cm' // true (or { } if the implementation does not support unit conversion) @2018-03-01 >= @2018-01-01 // true @2018-01-01 >= @2018-01-01 // true - equal with same precision @2018-03 >= @2018-03-01 // empty ({ }) - different precisions @2018-03-01T10:30:00 >= @2018-03-01T10:00:00 // true @2018-03-01T10 >= @2018-03-01T10:30 // empty ({ }) - different precisions @2018-03-01T10:30:00 >= @2018-03-01T10:30:00.0 // true - values are equal to seconds, trailing zeroes after the decimal are ignored @T10:30:00 >= @T10:00:00 // true @T10 >= @T10:30 // empty ({ }) - different precisions @T10:30:00 >= @T10:30:00.0 // true - values are equal to seconds, trailing zeroes after the decimal are ignored ``` -------------------------------- ### FHIRPath Date Addition Examples Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates adding time-valued quantities to Date and DateTime values in FHIRPath. It shows how calendar durations are respected, and how partial date/time values are handled when the quantity has higher precision. ```java @2014 + 24 months @2019-03-01 + 24 months // @2021-03-01 @2014 + 23 months @2016 + 365 days ``` -------------------------------- ### Check String Prefix (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Determines if a string begins with a specified prefix. Returns true if the prefix is an empty string. Returns empty if the input collection is empty. Errors if the input collection has multiple items. ```java 'abcdefg'.startsWith('abc') // true 'abcdefg'.startsWith('xyz') // false ``` -------------------------------- ### Extract Substring (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Returns a portion of a string starting from a specified zero-based index. An optional length parameter limits the number of characters returned. Handles out-of-bounds start indices and returns remaining characters if length exceeds available characters. Errors if the input collection has multiple items. ```java 'abcdefg'.substring(3) // 'defg' 'abcdefg'.substring(1, 2) // 'bc' 'abcdefg'.substring(6, 2) // 'g' 'abcdefg'.substring(7, 1) // { } ``` -------------------------------- ### Basic Path Selection in FHIRPath Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates simple path traversal in FHIRPath to select specific data elements. This is the most basic form of navigation. ```java name.given ``` -------------------------------- ### Proposed FHIRPath New Keyword Sorting Examples Source: https://github.com/hl7/fhirpath/wiki/Sort-Syntax-Proposal-Discussion Presents a proposal for new keywords (`asc`, `desc`) in FHIRPath's sort function to explicitly control the sort order for individual fields, including examples with numeric sorting using unary minus. ```FHIRPath Patient.name.sort(family desc, given.first() desc) Patient.name.sort(desc(family), desc(given.first())) Patient.name.sort(family.rank(), given.first().rank()) ( 1 | 2 | 3).sort(-$this) ( 1 | 2 | 3).sort($this desc) ( 1 | 2 | 3).sort(-$this desc) ``` -------------------------------- ### Get Integer Portion of a Number Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Returns the integer portion of the input number. Handles positive and negative numbers. ```java 101.truncate() // 101 1.00000001.truncate() // 1 (-1.56).truncate() // -1 ``` -------------------------------- ### String Manipulation Functions Source: https://github.com/hl7/fhirpath/blob/master/input/pages/index.md This section covers functions for replacing substrings, getting string length, converting to characters, encoding/decoding, escaping/unescaping, and trimming whitespace. ```APIDOC ## replaceMatches(regex : String, substitution: String, [flags : String]) : String ### Description Matches the input using the regular expression in `regex` and replaces each match with the `substitution` string. The substitution may refer to identified match groups in the regular expression. ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```fhirpath '11/30/1972'.replaceMatches('\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b', '${day}-${month}-${year}') 'aaabaa'.replaceMatches('aa', '"aa"') // returns "aa"ab"aa" ``` ### Response #### Success Response (String) - **result** (String) - The string with matches replaced. #### Response Example ```json { "example": "30-11-1972" } ``` ## length() : Integer ### Description Returns the number of characters in the input string. ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters None ### Request Example ```fhirpath 'abc'.length() ``` ### Response #### Success Response (Integer) - **result** (Integer) - The number of characters in the string. #### Response Example ```json { "example": 3 } ``` ## toChars() : collection ### Description Returns the list of characters in the input string. ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters None ### Request Example ```fhirpath 'abc'.toChars() ``` ### Response #### Success Response (Collection) - **result** (Collection) - A collection of characters from the string. #### Response Example ```json { "example": ["a", "b", "c"] } ``` ## encode(format : String) : String ### Description Encodes a singleton string in the specified format (e.g., hex, base64, urlbase64). ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters #### Query Parameters - **format** (String) - Required - The encoding format (e.g., 'hex', 'base64', 'urlbase64'). ### Request Example ```fhirpath 'hello'.encode('base64') ``` ### Response #### Success Response (String) - **result** (String) - The encoded string. #### Response Example ```json { "example": "aGVsbG8=" } ``` ## decode(format : String) : String ### Description Decodes a singleton encoded string according to the specified format. ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters #### Query Parameters - **format** (String) - Required - The decoding format (e.g., 'hex', 'base64', 'urlbase64'). ### Request Example ```fhirpath 'aGVsbG8='.decode('base64') ``` ### Response #### Success Response (String) - **result** (String) - The decoded string. #### Response Example ```json { "example": "hello" } ``` ## escape(target : String) : String ### Description Escapes a singleton string for a given target (e.g., 'html', 'json'). ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters #### Query Parameters - **target** (String) - Required - The target for escaping (e.g., 'html', 'json'). ### Request Example ```fhirpath '
'.escape('html') '"quoted"'.escape('json') ``` ### Response #### Success Response (String) - **result** (String) - The escaped string. #### Response Example ```json { "example": "<div>" } ``` ## unescape(target : String) : String ### Description Unescapes a singleton string for a given target. ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters #### Query Parameters - **target** (String) - Required - The target for unescaping (e.g., 'html', 'json'). ### Request Example ```fhirpath '<div>'.unescape('html') '\"quoted\"'.unescape('json') ``` ### Response #### Success Response (String) - **result** (String) - The unescaped string. #### Response Example ```json { "example": "
" } ``` ## trim() : String ### Description Trims whitespace characters from the beginning and ending of the input string. ### Method FHIRPath Function ### Endpoint N/A (In-expression function) ### Parameters None ### Request Example ```fhirpath ' hello '.trim() ``` ### Response #### Success Response (String) - **result** (String) - The trimmed string. #### Response Example ```json { "example": "hello" } ``` ``` -------------------------------- ### FHIRPath Existence Function Examples Source: https://context7.com/hl7/fhirpath/llms.txt Demonstrates FHIRPath functions used for checking the existence and properties of collections. Includes `empty()`, `exists()`, `all()`, `allTrue()`, `anyTrue()`, `allFalse()`, `anyFalse()`, `count()`, `distinct()`, and `isDistinct()`. ```fhirpath // empty() - returns true if collection is empty Patient.name.empty() // false if patient has names // exists([criteria]) - returns true if collection has elements Patient.name.exists() // true if any names exist Patient.identifier.exists(use = 'official') // true if official identifier exists Patient.telecom.exists(system = 'phone' and use = 'mobile') // all(criteria) - returns true if all elements match criteria generalPractitioner.all($this.resolve() is Practitioner) // allTrue() / anyTrue() / allFalse() / anyFalse() Observation.select(component.value > 90 'mm[Hg]').allTrue() Observation.select(component.value > 90 'mm[Hg]').anyTrue() // count() - returns number of items Patient.name.count() // Returns: 2 // distinct() - returns unique items only Patient.meta.tag.distinct() // isDistinct() - returns true if all items are unique Patient.identifier.isDistinct() ``` -------------------------------- ### FHIRPath Addition Operator Examples Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates the addition operator '+' in FHIRPath for Integer, Decimal, Quantity, and String types. For quantities, dimensions must match, but units can differ. For strings, it performs concatenation. ```java 3 'm' + 3 'cm' // 303 'cm' ``` -------------------------------- ### FHIRPath Date/DateTime Arithmetic Examples Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates decrementing Date and DateTime values by a time-valued quantity. It highlights how FHIRPath handles different units (years, months, days, etc.) and precisions, including cases where the quantity's precision exceeds the date/time's precision. ```java @2014 - 24 months @2019-03-01 - 24 months // @2017-03-01 ``` -------------------------------- ### FHIRPath Time Literals (Java) Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates the format for Time literals in FHIRPath. Time values start with '@T' and follow the hh:mm:ss.fff format, with optional milliseconds. ```java @T12:00 @T14:30:14.559 ``` -------------------------------- ### Calculate Logarithm with .log(base) in Java Source: https://github.com/hl7/fhirpath/blob/master/spec/index.adoc Demonstrates the .log(base) function in Java for calculating the logarithm of a number with a specified base. It handles implicit conversion of integers to decimals. ```java 16.log(2) // 4.0 100.0.log(10.0) // 2.0 ```