<set-var>

Variable setter processor

Core v2.2.0

Overview

The processor updates the value of an existing variable in the execution context. This is a legacy processor maintained for backward compatibility.

Usage Examples

Example 1: Basic variable update (legacy syntax)

example-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="counter" value="0"/>

<loop maxloops="10">
  <!-- Legacy: set-var -->
  <set-var name="counter">${counter + 1}</set-var>
  
  <!-- Modern: use def instead -->
  <def var="counter" value="${counter}1"/>
</loop>
</config>

Example 2: Arithmetic expressions (limited support)

example-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="count" value="5"/>

<!-- set-var has basic arithmetic -->
<set-var name="count">${count + 1}</set-var>
<set-var name="count">${count - 1}</set-var>
</config>

Example 3: Updating with processor result

example-3.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="totalItems" value="0"/>

<loop item="page" maxloops="5">
  <def var="pageItems">
    <xpath expression="count(//item)">
      <http url="https://api.example.com/page/${page}"/>
    </xpath>
  </def>
  
  <!-- Add to total (string concatenation, not arithmetic!) -->
  <set-var name="totalItems">${totalItems}${pageItems}</set-var>
</loop>
</config>

Important Notes

Related Processors