Variable definition plugin
Core v2.2.0
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
}).
<?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>
<?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>
<?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>
<?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>
<?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>
<?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>