Version 5.8.0

# `SetVariable`

Associates a value with a variable name. The value can be a simple value or a more complex one consisting of several elements.

## Child elements

`Attribute`, `ClearPage`, `Column`, `Columns`, `Copy-of`, `Element`, `ForAll`, `Loop`, `Ol`, `Output`, `Paragraph`, `PlaceObject`, `SortSequence`, `Switch`, `Table`, `TableNewPage`, `Tablehead`, `Tablerule`, `Td`, `Tr`, `Ul`, `Value`

## Parent elements

`AtPageCreation`, `AtPageShipout`, `Case`, `Contents`, `ForAll`, `Function`, `Include`, `Layout`, `Loop`, `Otherwise`, `Record`, `SavePages`, `Section`, `Table`, `Until`, `While`

## Attributes

`execute` (optional, since version 4.11.8)
  Execute the contents of setvariable now or execute it on usage.
  - `now`: Execute the contents during SetVariable (default).
  - `later`: Execute the contents when evaluated during `Copy-of`. Experimental.
`select` (XPath expression, optional)
  The value of the contents.
`trace` (optional)
  Show information about the assignment in the log file.
  - `yes`: Show information.
  - `no`: Don’t show information (default).
`type` (optional, since version 4.3.10)
  Set the data type of the variable. Currently only supported for MetaPost variables.
  - `sd:any`: The default (any) datatype for variables in the speedata layout language.
  - `mp:boolean`: A MetaPost boolean value.
  - `mp:cmykcolor`: A MetaPost CMYK color.
  - `mp:numeric`: A MetaPost numeric value.
  - `mp:string`: A MetaPost string value.
  - `mp:rgbcolor`: A MetaPost RGB color.
`variable` (text)
  The name of the variable that holds the contents.

## Remarks

Variables have global scope.

## Example

    <Record element="product">
      <SetVariable variable="wd" select="5"/>
      <PlaceObject>
        <Textblock width="{ $wd }">
          <Paragraph>
            <Value select="$articlenumber"/>
          </Paragraph>
        </Textblock>
      </PlaceObject>
    </Record>

The following example shows a more complex scenario: you can collect complex elements in a variable.

    <Record element="products">
      <SetVariable variable="articletext"/>
      <ProcessNode select="article"/>
      <PlaceObject>
        <Textblock>
          <Value select=" $articletext "/>
        </Textblock>
      </PlaceObject>
    </Record>

    <Record element="article">
      <SetVariable variable="articletext">
        <!-- the previous contents is added -->
        <Value select="$articletext"/>
        <Paragraph>
          <Value select=" @description "/>
        </Paragraph>
      </SetVariable>
    </Record>

#### On this page