1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-20 04:23:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xsl-array.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

116 lines
3.9 KiB
Markdown

---
title: "xsl:array"
description: "Creates an XDM array from xsl:array-member children, providing ordered positional access to sequences of arbitrary XDM values in XSLT 3.0."
date: 2026-04-18T00:00:00Z
version: "3.0"
versionLabel: "XSLT 3.0"
category: "element"
syntax: "<xsl:array><xsl:array-member .../></xsl:array>"
tags: ["xslt", "reference", "xslt3"]
---
## Description
`xsl:array` is the instruction for constructing an XDM array. Arrays are ordered, positionally indexed collections introduced in XDM 3.1. They differ from sequences in a critical way: each member of an array can itself be a sequence of zero or more items, whereas sequences are always flat. This makes arrays essential for representing JSON arrays (which can contain nested arrays and objects) and for passing multi-valued parameters to functions.
The instruction form `xsl:array` is used when the members must be computed dynamically. You populate it with `xsl:array-member` children, optionally interspersed with `xsl:for-each`, `xsl:if`, and other XSLT instructions. For simple cases where members are known at compile time, the XPath square-bracket syntax `[item1, item2]` is more concise.
An XDM array is a function from an integer position (1-based) to its member value. You access members with `array:get($arr, $pos)` or the shorthand `$arr($pos)`.
## Attributes
`xsl:array` has no element-specific attributes. Its content must consist of `xsl:array-member` instructions (and other instructions producing array members).
## Examples
### Creating an array from XML nodes
**Input XML:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<playlist>
<track>Shape of You</track>
<track>Blinding Lights</track>
<track>Levitating</track>
</playlist>
```
**Stylesheet:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:array="http://www.w3.org/2005/xpath-functions/array">
<xsl:output method="json" indent="yes"/>
<xsl:template match="/playlist">
<xsl:map>
<xsl:map-entry key="'title'" select="'My Playlist'"/>
<xsl:map-entry key="'tracks'">
<xsl:array>
<xsl:for-each select="track">
<xsl:array-member select="string(.)"/>
</xsl:for-each>
</xsl:array>
</xsl:map-entry>
</xsl:map>
</xsl:template>
</xsl:stylesheet>
```
**Output (JSON):**
```json
{
"title": "My Playlist",
"tracks": ["Shape of You", "Blinding Lights", "Levitating"]
}
```
### Array of sequences (each member holds multiple values)
**Stylesheet:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/data">
<xsl:variable name="matrix">
<xsl:array>
<xsl:array-member select="(1, 2, 3)"/>
<xsl:array-member select="(4, 5, 6)"/>
<xsl:array-member select="(7, 8, 9)"/>
</xsl:array>
</xsl:variable>
<result>
<!-- Access row 2, then second element of that sequence -->
<cell><xsl:value-of select="array:get($matrix, 2)[2]"/></cell>
</result>
</xsl:template>
</xsl:stylesheet>
```
**Output:**
```xml
<result>
<cell>5</cell>
</result>
```
## Notes
- Arrays are 1-indexed. `array:get($arr, 1)` retrieves the first member.
- Each member of an `xsl:array` is a *sequence*, not just a single item. This is what distinguishes arrays from flat sequences.
- The XPath shorthand `[ ]` creates an array where each member is a single item.
- To append or modify an array, use functions like `array:append()`, `array:put()`, or `array:insert-before()`.
- `xsl:array` can be used inside `xsl:map-entry` to create JSON-compatible nested structures.
## See also
- [xsl:array-member](../xsl-array-member)
- [array:get()](../xpath-array-get)
- [xsl:map](../xsl-map)