Chapter 24. Maps, Arrays, and JSON

This chapter covers maps and arrays, which are new to the data model in version 3.1. It describes how to construct, manipulate, and query them. Because the support for maps and arrays fits neatly with JSON objects and arrays, this chapter also describes support for parsing and serializing JSON.

Maps

A map is a collection of key/value pairs that can be constructed, manipulated, and queried in XQuery 3.1. Each key/value pair is known as an entry. Within a map, each key is unique and the order of the entries has no particular significance.

Maps are full-fledged items in the XQuery data model and can be constructed using map constructors. Special operators are provided for looking up values in maps, and they can also be queried and manipulated by a number of built-in functions, many of which are in the namespace http://www.w3.org/2005/xpath-functions/map, which is typically bound to the prefix map.

Constructing Maps

Maps can be constructed in queries by using either a map constructor or a built-in function, as described in this section. They can also be created by parsing a JSON object, as described later in the chapter.

Using a map constructor

Example 24-1 shows a simple map constructor that creates a map containing three entries.

Example 24-1. A simple map constructor
xquery version "3.1";
map {
  "ACC": "Accessories",
  "WMN": "Women's",
  "MEN": "Men's"
}

Figure 24-1 shows the syntax for a map constructor.

Figure 24-1. Syntax of a map constructor

The map constructor ...

Get XQuery, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.