XSLT - Maps and Arrays in XSLT 3.1

XSLT 3.1 introduced two powerful data structures called maps and arrays, which significantly expanded the language beyond traditional XML tree processing. Earlier versions of XSLT primarily worked with XML nodes and sequences, making it difficult to represent key-value data or ordered collections efficiently. Maps and arrays solve this limitation by providing modern data structures similar to those found in programming languages such as Java, Python, and JavaScript.

These features make XSLT suitable not only for XML transformations but also for handling JSON data, configuration settings, lookup tables, and structured datasets. Developers can now perform more complex data manipulations without relying on external programming languages.

Understanding Maps

A map is a collection of key-value pairs. Each key uniquely identifies a value, making data retrieval fast and organized. A key can be a string, number, or other atomic value, while the corresponding value can be almost anything, including strings, numbers, XML nodes, sequences, arrays, or even other maps.

For example, consider storing employee information:

  • Employee ID → E101

  • Name → Rahul

  • Department → Finance

  • Salary → 60000

Instead of searching through multiple XML elements, all this information can be stored inside a single map, allowing instant access to any value using its key.

Conceptually, the map looks like this:

{
    "EmployeeID": "E101",
    "Name": "Rahul",
    "Department": "Finance",
    "Salary": 60000
}

Maps provide a simple and efficient way to organize structured information.


Creating a Map

Maps are created using the map keyword.

Example:

<xsl:variable name="student" as="map(*)"
    select="
        map{
            'RollNo':'101',
            'Name':'Anita',
            'Course':'Computer Science',
            'Marks':92
        }"/>

In this example:

  • RollNo is a key.

  • 101 is its value.

  • Name is another key.

  • Anita is the corresponding value.

The variable now contains a complete collection of related information.


Accessing Values from a Map

Individual values are retrieved by specifying the key.

Example:

$student?Name

Output:

Anita

Similarly,

$student?Marks

Output:

92

The question mark operator is called the lookup operator because it retrieves the value associated with a key.


Updating a Map

Maps are immutable in XSLT.

This means the original map cannot be changed after it is created. Instead, a new map is generated with the updated values.

Example:

map:put($student, "Marks", 95)

Result:

{
    "RollNo":"101",
    "Name":"Anita",
    "Course":"Computer Science",
    "Marks":95
}

The original map still contains Marks as 92, while the new map contains 95.

This immutable approach prevents accidental modification of data and improves program reliability.


Removing Entries from a Map

Entries can also be removed.

Example:

map:remove($student, "Course")

Result:

{
    "RollNo":"101",
    "Name":"Anita",
    "Marks":92
}

The "Course" key no longer exists in the newly created map.


Checking Whether a Key Exists

Sometimes a transformation needs to determine whether a key is available before using it.

Example:

map:contains($student, "Name")

Output:

true

Example:

map:contains($student, "Phone")

Output:

false

This prevents errors when working with optional information.


Retrieving All Keys

All keys stored inside a map can be listed.

Example:

map:keys($student)

Output:

RollNo
Name
Course
Marks

This is useful when processing maps dynamically without knowing their structure beforehand.


Nested Maps

A map can contain another map as one of its values.

Example:

map{
    "Student":"Rahul",
    "Address":
        map{
            "City":"Bangalore",
            "State":"Karnataka",
            "Country":"India"
        }
}

To access the city:

$student?Address?City

Output:

Bangalore

Nested maps help represent hierarchical data in a compact and readable form.


Understanding Arrays

An array is an ordered collection of values.

Unlike maps, arrays use numerical positions rather than keys.

Example:

["Apple","Banana","Orange","Mango"]

Each item has a position.

Position Value
1 Apple
2 Banana
3 Orange
4 Mango

Arrays are useful whenever the order of items is important.


Creating an Array

Example:

<xsl:variable name="fruits"
    select="array{'Apple','Banana','Orange','Mango'}"/>

This creates an array containing four items.


Accessing Array Elements

Array elements are accessed by their position.

Example:

$array?2

Output:

Banana

Similarly,

$array?4

Output:

Mango

The indexing starts from 1.


Arrays Can Store Different Types

Arrays are flexible and can contain different kinds of data.

Example:

array{
    "Book",
    500,
    true(),
    current-date()
}

The array contains:

  • A string

  • A number

  • A Boolean value

  • A date

This flexibility allows arrays to represent diverse datasets.


Nested Arrays

Arrays can also contain other arrays.

Example:

array{
    "North",
    array{"East","West"},
    "South"
}

Accessing the nested value:

$array?2?1

Output:

East

Nested arrays are useful for representing multidimensional data such as matrices, seating arrangements, or grouped information.


Arrays Containing Maps

Maps and arrays can be combined.

Example:

array{
    map{
        "Name":"John",
        "Age":25
    },
    map{
        "Name":"David",
        "Age":30
    }
}

Retrieve the first employee's name:

$array?1?Name

Output:

John

This structure closely resembles JSON arrays of objects and is commonly used in modern data processing.


Working with JSON

One of the biggest advantages of maps and arrays is their seamless integration with JSON.

Consider the following JSON document:

{
    "name":"Ravi",
    "city":"Mysore",
    "skills":["XML","XSLT","XPath"]
}

When parsed in XSLT 3.1:

  • The JSON object becomes a map.

  • The skills list becomes an array.

Accessing values becomes straightforward.

Retrieve the name:

$data?name

Output:

Ravi

Retrieve the second skill:

$data?skills?2

Output:

XSLT

This makes XSLT highly effective for processing modern web service data.


Advantages of Maps and Arrays

Maps and arrays provide several benefits:

  • Efficient organization of structured data.

  • Fast retrieval of information using keys or positions.

  • Excellent support for JSON processing.

  • Ability to represent complex hierarchical structures.

  • Improved readability and maintainability of XSLT code.

  • Better integration with web APIs and REST services.

  • Support for functional programming techniques.

  • Reduced dependency on XML-only data models.

  • Simplified handling of configuration files and lookup tables.

  • Increased flexibility for modern data transformation tasks.

Practical Applications

Maps and arrays are widely used in modern XSLT applications, including:

  • Processing JSON responses from REST APIs.

  • Storing application configuration settings.

  • Creating lookup tables for data transformation.

  • Managing employee, customer, or product information.

  • Transforming data between XML and JSON formats.

  • Building reusable libraries for enterprise applications.

  • Handling nested and hierarchical business data.

  • Processing cloud-based service responses.

  • Organizing multilingual translation resources.

  • Developing scalable and maintainable XSLT solutions for enterprise environments.

Conclusion

Maps and arrays represent one of the most significant enhancements introduced in XSLT 3.1. They extend the language beyond traditional XML node manipulation by enabling efficient storage and processing of structured data. Maps provide quick access to information through key-value pairs, while arrays maintain ordered collections of items. Together, they simplify JSON handling, support functional programming techniques, and make XSLT a more versatile language for modern application development and data transformation. Understanding these data structures is essential for developers who want to leverage the full capabilities of XSLT 3.1 in enterprise and web-based environments.