> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lerian.studio/llms.txt
> Use this file to discover all available pages before exploring further.

# XML template

> Build an analytical financial report in XML with Reporter — account-level activity, custom logic like discounts, and per-account summaries.

This page guides you through a full example of an **Analytical Financial** report in **XML**. The template breaks down account-level activity, applies custom logic (like discounts), and includes clear summaries to support analysis and auditing.

**What this report shows:**

* **Organization and Ledger details**.
* **Account-level overview**: balance, currency, and alias.
* **Operation breakdowns**: original amount, discount, final amount, status, description, and type.
* **Summaries per account**: total operations, total value, and average per operation.

## Template code

***

<CodeGroup>
  ```xml XML expandable theme={null}
  <AnalyticalReport>
      <Organization>{{ midaz_onboarding.legal_name }} - Tax ID: {{ midaz_onboarding.legal_document }}</Organization>
      <GenerationDate>{% date_time "dd/MM/YYYY HH:mm" %}</GenerationDate>
      {%- with ledger = midaz_onboarding.ledger[0] %}
      <Ledger>{{ ledger.name }}</Ledger>

      {%- for account in midaz_onboarding.account %}
      <Account>
          <AccountID>{{ account.id }}</AccountID>
          <Alias>{{ account.alias }}</Alias>
          {%- with balance = filter(midaz_transaction.balance, "account_id", account.id)[0] %}
          <CurrentBalance> {{ balance.available }}</CurrentBalance>
          {%- endwith %}
          {%- for balance in midaz_transaction.balance %}
          {%- if balance.account_id == account.id %}
          <CurrentBalance>{{ balance.available }}</CurrentBalance>
          {%- endif %}
          {%- endfor %}
          <Currency>{{ account.asset_code }}</Currency>
          <Operations>
          {%- for operation in midaz_transaction.operation %}
          {%- if operation.account_id == account.id %}
              {%- set original_amount = operation.amount %}
              {%- set discount_amount = original_amount * 0.03 %}
              {%- set final_amount = original_amount - discount_amount %} 
          <Operation>
                  <OperationID>{{ operation.id }}</OperationID>
                  <Description>{{ operation.description }}</Description>
                  <Type>{{ operation.type }}</Type>
                  <Route>{{ operation.route }}</Route>
                  <OriginalAmount>{{ original_amount }}</OriginalAmount>
                  <DiscountAmount>{{ discount_amount }}</DiscountAmount>
                  <FinalAmountWithDiscount>{{ final_amount }}</FinalAmountWithDiscount>
                  <Currency>{{ operation.asset_code }}</Currency>
                  <Status>{{ operation.status }}</Status>
              </Operation>
          {%- endif %}
          {%- endfor %}
          </Operations>
          <AccountSummary>
              <TotalOperations>{% count_by midaz_transaction.operation if account_id == account.id %}</TotalOperations>
              <SumOfOperations>{% sum_by midaz_transaction.operation by "amount" if account_id == account.id %}</SumOfOperations>
              <AverageOfOperations>{% avg_by midaz_transaction.operation by "amount" if account_id == account.id %}</AverageOfOperations>
          </AccountSummary>
      </Account>
      {%- endfor %}
  </AnalyticalReport>
  ```
</CodeGroup>

## Code breakdown

***

### Organization and Ledger information

<CodeGroup>
  ```xml XML theme={null}
  <AnalyticalReport>
      <Organization>{{ midaz_onboarding.legal_name }} - Tax ID: {{ midaz_onboarding.legal_document }}</Organization>
      <GenerationDate>28.04.2025</GenerationDate>
      {%- with ledger = midaz_onboarding.ledger[0] %}
      <Ledger>{{ ledger.name }}</Ledger>
  ```
</CodeGroup>

**`<Organization>{{ midaz_onboarding.organization.legal_name }} - CNPJ: {{ midaz_onboarding.organization.legal_document }}</Organization>`**

Displays the organization’s legal name and corresponding *CNPJ* (Brazilian Tax ID).

* `{{ midaz_onboarding.organization.legal_name }}`: retrieves the organization's name.
* `{{ midaz_onboarding.legal_document }}`: retrieves the *CNPJ*.

**`<GenerationDate>{% date_time "dd/MM/YYYY HH:mm" %}</GenerationDate>`**
This function applies the date and time when the template is rendered, using the `dd/MM/YYYY HH:mm` format.

**`{%- with ledger = midaz_onboarding.ledger[0] %}`**

Creates a temporary variable pointing to the first available ledger. This keeps the code cleaner and avoids repetitive references.

* `-`: removes extra spaces in the rendered file.
* `ledger`: the name of the temporary variable.
* `midaz_onboarding.ledger[0].name`: points to the first available ledger.

### Looping through accounts

<CodeGroup>
  ```xml XML theme={null}
      {%- for account in midaz_onboarding.account %}
      <Account>
          <AccountID>{{ account.id }}</AccountID>
          <Alias>{{ account.alias }}</Alias>
          {%- with balance = filter(midaz_transaction.balance, "account_id", account.id)[0] %}
          <CurrentBalance> {{ balance.available }}</CurrentBalance>
          {%- endwith %}
  ```
</CodeGroup>

**`{%- for account in midaz_onboarding.account %}` ... `{% endfor %}`**

Loops through all accounts linked to the user. Since most users have more than one account, this allows you to display individual data for each one.

* `midaz_onboarding.account`: the full list of user accounts.
* `{%- for account in midaz_onboarding.account %}`: the loop runs once per account.

**`{%- with balance = filter(midaz_transaction.balance, "account_id", account.id)[0] %}`**

Defines a variable named `balance` that holds the current account’s balance.

* `filter()`: scans through `midaz_transaction.balance` to find the one matching the `account.id`.
* `[0]`: grabs the first match.
* The result is stored in `balance`.

**`<CurrentBalance> {{ balance.available }}</CurrentBalance>`**

Shows the account’s available balance (`balance.available`).

### Looping without `{%with...%}`

<CodeGroup>
  ```xml XML theme={null}
          {%- for balance in midaz_transaction.balance %}
          {%- if balance.account_id == account.id %}
          <CurrentBalance>{{ balance.available }}</CurrentBalance>
          {%- endif %}
          {%- endfor %}
  ```
</CodeGroup>

**`{%- for balance in midaz_transaction.balance %}`**

Loops through all balances to find the one that matches the current account. This is a more explicit alternative to `filter()`.

* `midaz_transaction.balance`: The list of all balances.
* `{%- balance in midaz_transaction.balance %}`: the loop runs once per balance.

**`{% if balance.account_id == account.id %}`**

Filters balances to show only those that match the current account being processed.

* If they match, the balance is displayed.

### Currency information

<CodeGroup>
  ```xml XML theme={null}
         <Currency>{{ account.asset_code }}</Currency>
  ```
</CodeGroup>

Shows the account’s currency (e.g., BRL, USD) to help you understand the value context.

* `account.asset_code`: dynamically injects the currency code.

### Grouping operations

<CodeGroup>
  ```xml XML theme={null}
          <Operations>
          {%- for operation in midaz_transaction.operation %}
          {%- if operation.account_id == account.id %}
              {%- set original_amount = operation.amount %}
              {%- set discount_amount = original_amount * 0.03 %}
              {%- set final_amount = original_amount - discount_amount %} 
  ```
</CodeGroup>

### `<Operations>...</Operations>`

Defines the section where all operations for an account are listed.

* Keeps the report structured and easy to scan.

**`{%- for operation in midaz_transaction.operation %}`**

Loops through all operations in the transaction, and displays operations that match the current account.

* `operation`: represents the current operation that is part of the current transaction.
* `midaz_transaction.operation`: the list of all operations in the transaction.

**`{%- if operation.account_id == account.id %}`**

Ensures that only the relevant operations for the current account are included.

* If the `account_id` of the operation is equal to the current `account.id`, the rest of the block will be executed.

**`{%- set original_amount = operation.amount %}`**

Defines the original amount and sets it as a variable.

* `original_amount`: the name of the variable.

**`{%- set discount_amount = original_amount * 0.03 %}`**

Defines a variable and calculates a 3% discount on the original amount.

* `discount_amount`: the name of the variable.
* `original_amount * 0.03`: applies a 3% discount on the original amount.

**`{%- set final_amount = original_amount - discount_amount %}`**

Subtracts the discount from the original amount to get the final value.

* `final_amount`: variable that represents the final value.
* `original_amount - discount_amount`: the subtraction operation.

### Operation block

<CodeGroup>
  ```xml XML theme={null}
          <Operation>
                  <OperationID>{{ operation.id }}</OperationID>
                  <Description>{{ operation.description }}</Description>
                  <Type>{{ operation.type }}</Type>
                  <Route>{{ operation.route }}</Route>
                  <OriginalAmount>{{ original_amount }}</OriginalAmount>
                  <DiscountAmount>{{ discount_amount }}</DiscountAmount>
                  <FinalAmountWithDiscount>{{ final_amount }}</FinalAmountWithDiscount>
                  <Currency>{{ operation.asset_code }}</Currency>
                  <Status>{{ operation.status }}</Status>
          </Operation>
  ```
</CodeGroup>

Each `<Operation>` contains detailed information about the transaction. These fields help users audit and understand what happened.

**`<OperationID>{{ operation.id }}</OperationID>`**

Unique ID of the operation.

**`<Description>{{ operation.description }}</Description>`**

Short description of the operation.

**`<Type>{{ operation.type }}</Type>`**

Operation type (e.g., credit, debit, adjustment).

**`<Route>{{ operation.route }}</Route>`**

Associated operation route entry.

**`<OriginalAmount>{{ original_amount }}</OriginalAmount>`**

Original amount before any adjustments.

**`<DiscountAmount>{{ discount_amount }}</DiscountAmount>`**

Value of the discount applied.

**`<FinalAmountWithDiscount>{{ final_amount }}</FinalAmountWithDiscount>`**

Final amount after discount.

**`<Currency>{{ operation.asset_code }}</Currency>`**

The currency used for the operation.

**`<Status>{{ operation.status }}</Status>`**

Operation status (e.g., Approved, pending).

### Account summary block

<CodeGroup>
  ```xml XML theme={null}
  <AccountSummary>
              <TotalOperations>{% count_by midaz_transaction.operation if account_id == account.id %}</TotalOperations>
              <SumOfOperations>{% sum_by midaz_transaction.operation by "amount" if account_id == account.id %}</SumOfOperations>
              <AverageOfOperations>{% avg_by midaz_transaction.operation by "amount" if account_id == account.id %}</AverageOfOperations>
          </AccountSummary>
  ```
</CodeGroup>

**`{% count_by midaz_transaction.operation if account_id == account.id %}`**

Counts the number of operations associated with your account and assists in understanding the transaction volume for the reporting period.

* The `count_by` function goes through `midaz_transaction.operation` and adds up how many operations have the same `account_id` as the current account.

**`{% sum_by midaz_transaction.operation by "amount" if account_id == account.id %}`**

Sums up the total value of all operations for this account, and outputs the result using two decimal places.

* `sum_by` loops through transactions filtered by `account_id`.
* Sums the `amount` fields.

**`{% avg_by midaz_transaction.operation by "amount" if account_id == account.id %}`**

Calculates the average operation value for this account. Useful for spotting spending patterns or outliers.

* `avg_by` applies the average calculation to the amount values.
* Only considers operations for the current account.

## Template output

***

<CodeGroup>
  ```xml XML expandable theme={null}
  <AnalyticalReport>
  	<Organization>Ferry, Stiedemann and Jast - CNPJ: 78425230000190</Organization>
  	<GenerationDate>28.04.2025</GenerationDate>
  	<Ledger>O'Connell, Dietrich and Bernhard</Ledger>
  	<Account>
  		<AccountID>01965efe-5afd-733d-9a64-3d53f73b13c8</AccountID>
  		<Alias>@external/BRL</Alias>
  		<CurrentBalance>1000.00</CurrentBalance>
  		<CurrentBalance>1000.00</CurrentBalance>
  		<Currency>BRL</Currency>
  		<Operations>
  			<Operation>
  				<OperationID>01965f04-7087-73f1-a802-f8fceedb34ec</OperationID>
  				<Description>Initial transfer</Description>
  				<Type>DEBIT</Type>
  				<Route></Route>
  				<OriginalAmount>1000.00</OriginalAmount>
  				<DiscountAmount>30.00</DiscountAmount>
  				<FinalAmountWithDiscount>970.00</FinalAmountWithDiscount>
  				<Currency>BRL</Currency>
  				<Status></Status>
  			</Operation>
  		</Operations>
  		<AccountSummary>
  			<TotalOperations>1</TotalOperations>
  			<SumOfOperations>1000.00</SumOfOperations>
  			<AverageOfOperations>1000.00</AverageOfOperations>
  		</AccountSummary>
  	</Account>
  	<Account>
  		<AccountID>01965eff-0b2d-73cf-bbfe-0dc1ac9032d3</AccountID>
  		<Alias>@wallet_46040127</Alias>
  		<CurrentBalance> 0</CurrentBalance>
  		<CurrentBalance> 0</CurrentBalance>
  		<Currency>BRL</Currency>
  		<Operations></Operations>
  		<AccountSummary>
  			<TotalOperations>0</TotalOperations>
  			<SumOfOperations>0.00</SumOfOperations>
  			<AverageOfOperations>0.00</AverageOfOperations>
  		</AccountSummary>
  	</Account>
  	<Account>
  		<AccountID>01965f01-aa72-7254-bb4a-fa9c8c9d4009</AccountID>
  		<Alias>@account1</Alias>
  		<CurrentBalance>300.00</CurrentBalance>
  		<CurrentBalance>300.00</CurrentBalance>
  		<Currency>BRL</Currency>
  		<Operations>
  			<Operation>
  				<OperationID>01965f04-7087-73d2-a167-cb5146bee801</OperationID>
  				<Description>External to accounts 1 and 2</Description>
  				<Type>CREDIT</Type>
  				<ChartOfAccounts></ChartOfAccounts>
  				<OriginalAmount>300.00</OriginalAmount>
  				<DiscountAmount>9.00</DiscountAmount>
  				<FinalAmountWithDiscount>291.00</FinalAmountWithDiscount>
  				<Currency>BRL</Currency>
  				<Status></Status>
  			</Operation>
  		</Operations>
  		<AccountSummary>
  			<TotalOperations>1</TotalOperations>
  			<SumOfOperations>300.00</SumOfOperations>
  			<AverageOfOperations>300.00</AverageOfOperations>
  		</AccountSummary>
  	</Account>
  	<Account>
  		<AccountID>01965f01-ff74-712a-9e2c-d987f15a1d14</AccountID>
  		<Alias>@account2</Alias>
  		<CurrentBalance>700.00</CurrentBalance>
  		<CurrentBalance>700.00</CurrentBalance>
  		<Currency>BRL</Currency>
  		<Operations>
  			<Operation>
  				<OperationID>01965f04-7087-73b1-aff9-69f59d623d31</OperationID>
  				<Description>External to accounts 1 and 2</Description>
  				<Type>CREDIT</Type>
  				<ChartOfAccounts></ChartOfAccounts>
  				<OriginalAmount>700.00</OriginalAmount>
  				<DiscountAmount>21.00</DiscountAmount>
  				<FinalAmountWithDiscount>679.00</FinalAmountWithDiscount>
  				<Currency>BRL</Currency>
  				<Status></Status>
  			</Operation>
  		</Operations>
  		<AccountSummary>
  			<TotalOperations>1</TotalOperations>
  			<SumOfOperations>700.00</SumOfOperations>
  			<AverageOfOperations>700.00</AverageOfOperations>
  		</AccountSummary>
  	</Account>
  	<Account>
  		<AccountID>01965f02-55fc-7a06-9f95-07e9a9ff4941</AccountID>
  		<Alias>@account3</Alias>
  		<CurrentBalance> 0</CurrentBalance>
  		<CurrentBalance> 0</CurrentBalance>
  		<Currency>BRL</Currency>
  		<Operations></Operations>
  		<AccountSummary>
  			<TotalOperations>0</TotalOperations>
  			<SumOfOperations>0.00</SumOfOperations>
  			<AverageOfOperations>0.00</AverageOfOperations>
  		</AccountSummary>
  	</Account>
  </AnalyticalReport>
  ```
</CodeGroup>
