# One query language for XML and JSON

Zorba is a C++ library and command-line tool that runs XQuery and JSONiq: declarative languages for selecting, joining, grouping, transforming and updating tree-shaped data.

This site explains what those languages actually cover, then teaches them by example. Every example is executed against the current Zorba build before the site is published, and the result you see is the result Zorba produced.

**XQuery**: `intro/both.xq`

```xquery
(: One query, two kinds of trees: an XML element and a JSON object. :)
let $book := doc("../data/books.xml")//book[@id = "b3"]
return {
  "title": string($book/title),
  "year": xs:integer($book/@year),
  "tags": [ $book/tags/tag/string() ]
}
```

**Result**: `zorba -i -r -f -q both.xq`

```
{
  "title" : "Neuromancer", 
  "year" : 1984, 
  "tags" : [ "novel", "science fiction" ]
}
```

## What Zorba is

Zorba is a query processor, not a database server. You give it a query and some data (files, strings, URLs, values from your program) and it returns a sequence of results, serialized as XML, JSON, HTML or text. It has three faces:

- a **command-line tool**, `zorba`, for running queries from files or the shell;
- a **C++ library** with a small API to compile a query once, bind variables and iterate over results;
- an **in-memory store** with collections, value indexes and integrity constraints, so a query can keep state between statements of a program.

It implements XQuery 3.0 with the Update Facility, Full Text and Zorba's scripting extension, and JSONiq 1.0, both as a language of its own and as an extension that adds JSON objects and arrays to XQuery. [The big picture](map.md) explains what each of those is and how they relate.

**XQuery**: `intro/hello.xq`

```xquery
let $who := "world"
return <greeting lang="en">Hello, {$who}!</greeting>
```

**Result**: `zorba -i -r -f -q hello.xq`

```
<greeting lang="en">Hello, world!</greeting>
```

**Why it matters:** XML is built with XML syntax. There is no string concatenation and no escaping to get wrong; the expression in braces is evaluated and inserted.

**JSONiq**: `intro/hello-json.jq`

```jsoniq
[
  for $n in 1 to 3
  return { "n": $n, "square": $n * $n }
]
```

**Result**: `zorba -i -r -f -q hello-json.jq`

```
[ {
  "n" : 1, 
  "square" : 1
}, {
  "n" : 2, 
  "square" : 4
}, {
  "n" : 3, 
  "square" : 9
} ]
```

**Why it matters:** the same loop produces JSON just as directly. In JSONiq, JSON objects and arrays are values like numbers and strings.

## Good fit, poor fit

### Good fit

- Reshaping documents: XML to HTML, XML to JSON, JSON to XML, one schema to another.
- Querying nested data where SQL needs many joins or JSON path tools run out of power: grouping, windowing, joins across documents.
- Embedding a standard, well-specified query language in a C++ application.
- Validating XML against XML Schema and working with typed data.
- Text search inside documents with stemming, wildcards, proximity and boolean operators.

### Poor fit

- A persistent, multi-user database. The store is in memory and lives as long as the process.
- Datasets far larger than RAM. Zorba streams some inputs, but documents are generally materialized.
- Code that needs XQuery 3.1 maps, arrays, `=>` or `?` lookups. Zorba predates 3.1; it covers JSON through JSONiq instead (see [JSONiq and XQuery 3.1](jsoniq.md#vs31)).
- XSLT. Zorba runs XQuery, not stylesheets.

## Quick start

The fastest way to try Zorba is the container image published by the project. Run a query given on the command line:

```sh
docker run --rm ghcr.io/zorba-processor/zorba:edge -r -q '1 + 1'
```

This prints `2` with no final newline. `-r` omits the XML declaration, which is otherwise written before the result.

Or mount the current directory and run a query file. The extension decides the language: `.xq` is XQuery, `.jq` is JSONiq.

```sh
echo 'for $i in 1 to 3 return <n>{$i * $i}</n>' > query.xq
docker run --rm -v "$PWD":/work ghcr.io/zorba-processor/zorba:edge -i -r -f -q query.xq
```

`-f` says the argument of `-q` is a file, `-i` indents the output. The examples on this site run with `zorba -i -r -f -q file`; their files are in [site/examples](https://github.com/zorba-processor/zorba/tree/master/site/examples). More flags are described in [Using Zorba](zorba.md#cli).

## Build from source

Zorba builds with CMake on Linux and macOS and needs a C++23 compiler, ICU, libxml2, libcurl and, for XML Schema support, Xerces-C. The repository's [README](https://github.com/zorba-processor/zorba#readme) lists the packages for Debian, Ubuntu and Homebrew and the CMake presets to use.

## How to read this site

[**The big picture**The data model and the family of specifications, with what Zorba implements.](map.md) [**XQuery**Paths, FLWOR, constructors, functions, types, errors, updates, full text.](xquery.md) [**JSONiq**Objects, arrays, navigation, null, updates, mixing JSON with XML.](jsoniq.md) [**Using Zorba**The CLI, built-in modules, collections and indexes, scripting, C++ embedding.](zorba.md) [**AI agents (MCP)**Query a folder of XML, JSON, CSV and HTML files from an AI agent with zorba-mcp.](mcp.md) [**Cheat sheet**The syntax on one page.](cheatsheet.md)

Each example names the file it comes from. The examples and their data live in [`site/examples`](https://github.com/zorba-processor/zorba/tree/master/site/examples) in the repository; the examples share two data files, [`books.xml`](examples/data/books.xml) and [`orders.json`](examples/data/orders.json).
