<xq-expression>

XQuery expression definition

Core v2.2.0

Overview

The processor defines the XQuery expression for complex XML processing. Must be used as a child element of . Supports XQuery 1.0 syntax with Saxon engine.

Usage Examples

Example 1: Basic XQuery with parameters

example-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<xquery>
  <xq-expression>
    <![CDATA[
      for $p in //product
      where $p/price > $minPrice
      return $p/name
    ]]>
  </xq-expression>
  <xq-param name="minPrice" type="double">100</xq-param>
</xquery>
</config>

Example 2: Complex data transformation

example-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<xquery>
  <xq-expression>
    <![CDATA[
      <products>
        {
          for $p in //product
          let $discount := if ($p/category = "electronics") then 0.1 else 0.05
          return
            <product>
              <name>{$p/name/text()}</name>
              <originalPrice>{$p/price/text()}</originalPrice>
              <discountedPrice>{$p/price * (1 - $discount)}</discountedPrice>
              <category>{$p/category/text()}</category>
            </product>
        }
      </products>
    ]]>
  </xq-expression>
</xquery>
</config>

Example 3: Aggregation and grouping

example-3.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<xquery>
  <xq-expression>
    <![CDATA[
      <summary>
        {
          for $category in distinct-values(//product/category)
          let $products := //product[category = $category]
          return
            <category name="{$category}">
              <count>{count($products)}</count>
              <avgPrice>{avg($products/price)}</avgPrice>
              <maxPrice>{max($products/price)}</maxPrice>
            </category>
        }
      </summary>
    ]]>
  </xq-expression>
</xquery>
</config>

Example 4: External variable binding

example-4.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="searchTerm">laptop</def>
<def var="maxPrice">2000</def>

<xquery>
  <xq-expression>
    <![CDATA[
      declare variable $search as xs:string external;
      declare variable $maxPrice as xs:double external;
      
      <results>
        {
          for $p in //product
          where contains(lower-case($p/name), lower-case($search))
            and $p/price <= $maxPrice
          order by $p/price ascending
          return $p
        }
      </results>
    ]]>
  </xq-expression>
  <xq-param name="search" type="string">${searchTerm}</xq-param>
  <xq-param name="maxPrice" type="double">${maxPrice}</xq-param>
</xquery>
</config>

Parameters

Important Notes

Related Processors