<xquery>

XQuery expression processor

Core v2.2.0

Overview

The processor evaluates XQuery expressions against XML data, providing advanced querying, filtering, transformation, and aggregation capabilities beyond what XPath offers. XQuery is a full-featured query language (similar to SQL for databases) designed specifically for XML data.

Usage Examples

Example 1: FLWOR expression (For-Let-Where-Order-Return)

example-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="expensiveProducts">
  <xquery>
    <![CDATA[
      for $product in //product
      where $product/price > 100
      order by $product/price descending
      return $product/name/text()
    ]]>
    <html-to-xml>
      <http url="https://example.com/products"/>
    </html-to-xml>
  </xquery>
</def>
</config>

Example 2: XML transformation

example-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="transformedXml">
  <xquery>
    <![CDATA[
      <products>
      {
        for $p in //div[@class='product']
        return
          <product>
            <id>{data($p/@id)}</id>
            <name>{$p//h3/text()}</name>
            <price>{$p//span[@class='price']/text()}</price>
          </product>
      }
      </products>
    ]]>
    <html-to-xml>
      <http url="https://example.com/products"/>
    </html-to-xml>
  </xquery>
</def>
</config>

Example 3: Aggregation (count, sum, avg)

example-3.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="totalPrice">
  <xquery>
    <![CDATA[
      sum(//product/price)
    ]]>
    <json-to-xml>
      <http url="https://api.example.com/products"/>
    </json-to-xml>
  </xquery>
</def>
</config>

Example 4: XQuery with node() parameter (v2.2.0 - Fixed r825)

example-4.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<!-- Create sample XML data -->
<def var="productsXml">
  <template><![CDATA[
    <products>
      <product><name>Book A</name><price>25.50</price></product>
      <product><name>Book B</name><price>18.99</price></product>
    </products>
  ]]></template>
</def>

<!-- Process with XQuery using node() parameter -->
<def var="analysis">
  <xquery>
    <xq-param name="data" type="node()">
      <get var="productsXml"/>
    </xq-param>
    <xq-expression>
      <![CDATA[
      declare variable $data as node() external;
      
      let $count := count($data//product)
      let $total := sum($data//product/price)
      
      return
      <analysis>
        <total_products>{$count}</total_products>
        <total_value>{$total}</total_value>
      </analysis>
      ]]>
    </xq-expression>
  </xquery>
</def>
</config>

Example 5: XQuery with typed parameters

example-5.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<xquery>
  <xq-param name="a" type="integer">10</xq-param>
  <xq-param name="b" type="integer">5</xq-param>
  <xq-expression>
    <![CDATA[
    declare variable $a as xs:integer external;
    declare variable $b as xs:integer external;
    
    <calculations>
      <sum>{$a + $b}</sum>
      <product>{$a * $b}</product>
    </calculations>
    ]]>
  </xq-expression>
</xquery>
</config>

Important Notes

Related Processors