<def>

Variable definition plugin

Core v2.2.0

Overview

The element is one of the most fundamental processors in WebHarvest. It creates or updates variables in the current scope, making data available for subsequent processors through variable references (${varname}).

Usage Examples

Example 1: Simple string value

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

<!-- Use the variable -->
<http url="https://api.example.com?key=${apiKey}"/>
</config>

Example 2: Value from child processor (HTTP response)

example-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="htmlContent">
  <http url="https://example.com"/>
</def>

<!-- Extract data from stored HTML -->
<def var="title">
  <xpath expression="//title/text()">
    <html-to-xml>${htmlContent}</html-to-xml>
  </xpath>
</def>
</config>

Example 3: Template interpolation in value attribute

example-3.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="baseUrl" value="https://example.com"/>
<def var="apiPath" value="/api/v1"/>

<!-- Combine variables -->
<def var="fullUrl" value="${baseUrl}${apiPath}/users"/>
<!-- Result: https://example.com/api/v1/users -->
</config>

Example 4: Text content with interpolation

example-4.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="firstName" value="John"/>
<def var="lastName" value="Doe"/>

<def var="fullName">Hello, ${firstName} ${lastName}!</def>
<!-- Result: Hello, John Doe! -->
</config>

Example 5: Complex value from multiple processors

example-5.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="productData">
  <http url="https://api.example.com/product/123"/>
  <json-to-xml>${http}</json-to-xml>
  <xpath expression="//product/name/text()"/>
</def>
</config>

Example 6: Updating existing variables

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

<loop item="product" index="i" maxloops="100">
  <http url="https://api.example.com/products"/>
  
  <!-- Update counter (concatenation, not arithmetic) -->
  <def var="counter" value="${counter}1"/>
</loop>
</config>

Important Notes

Related Processors