> For the complete documentation index, see [llms.txt](https://php-fhir-tools.ardenexal.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php-fhir-tools.ardenexal.net/fhirpath/functions/navigation.md).

# Tree Navigation & Utility

## Tree navigation

| Function             | Description                                                                                                     | Example                      |
| -------------------- | --------------------------------------------------------------------------------------------------------------- | ---------------------------- |
| `children()`         | Returns all direct child nodes of each input item.                                                              | `Patient.children()`         |
| `descendants()`      | Returns all descendant nodes recursively (shorthand for `repeat(children())`).                                  | `Patient.descendants()`      |
| `repeat(projection)` | Applies the projection repeatedly, unioning results until no new items appear (transitive closure). Cycle-safe. | `Questionnaire.repeat(item)` |

## Utility

| Function                             | Description                                                                               | Example                                      |
| ------------------------------------ | ----------------------------------------------------------------------------------------- | -------------------------------------------- |
| `trace(name [, projection])`         | Logs diagnostic output and returns the input unchanged.                                   | `Patient.name.trace('names').given`          |
| `aggregate(aggregator [, init])`     | Reduces a collection to a single value; exposes `$this`, `$index`, and `$total`.          | `value.aggregate($this + $total, 0)`         |
| `iif(condition, ifTrue [, ifFalse])` | Conditional expression; returns `ifTrue` when the condition is true, otherwise `ifFalse`. | `iif(active, 'yes', 'no')`                   |
| `sort([key, ...])`                   | Sorts by natural order, or by one or more projection keys (use unary `-` for descending). | `name.given.sort()` / `item.sort(-priority)` |

{% hint style="info" %}
`trace()` writes to a PSR-3 logger when one is configured via `FHIRPathEvaluator::setLogger()`, otherwise it falls back to PHP's `error_log()`.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://php-fhir-tools.ardenexal.net/fhirpath/functions/navigation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
