Sponsored

XSLT Reference

array:get()

XSLT 3.0 array function

Returns the member of an array at a specified 1-based position.

Syntax
array:get(array, position)

Description

array:get() retrieves the member at the given 1-based integer position in an array. If the position is less than 1 or greater than the array size, error err:FOAY0001 is raised. An alternative shorthand is $array($position) using function-call syntax.

Parameters

ParameterTypeRequiredDescription
arrayarray(*)YesThe array to access.
positionxs:integerYesThe 1-based position of the member to retrieve.

Return value

item()* — the member at the given position (may be a sequence if the member is a sequence).

Examples

Accessing array elements by position

Stylesheet:

<?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="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="colors" select="['red', 'green', 'blue', 'yellow']"/>
    <result>
      <!-- Both syntaxes are equivalent -->
      <first><xsl:value-of select="array:get($colors, 1)"/></first>
      <third><xsl:value-of select="$colors(3)"/></third>
      <last><xsl:value-of select="array:get($colors, array:size($colors))"/></last>
    </result>
  </xsl:template>
</xsl:stylesheet>

Output:

<result>
  <first>red</first>
  <third>blue</third>
  <last>yellow</last>
</result>

Iterating with positional access

Stylesheet:

<?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="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="matrix" select="[[1,2,3],[4,5,6],[7,8,9]]"/>
    <matrix>
      <xsl:for-each select="1 to array:size($matrix)">
        <xsl:variable name="row-idx" select="."/>
        <row n="{$row-idx}">
          <xsl:variable name="row" select="array:get($matrix, $row-idx)"/>
          <xsl:for-each select="1 to array:size($row)">
            <cell><xsl:value-of select="array:get($row, .)"/></cell>
          </xsl:for-each>
        </row>
      </xsl:for-each>
    </matrix>
  </xsl:template>
</xsl:stylesheet>

Output:

<matrix>
  <row n="1"><cell>1</cell><cell>2</cell><cell>3</cell></row>
  <row n="2"><cell>4</cell><cell>5</cell><cell>6</cell></row>
  <row n="3"><cell>7</cell><cell>8</cell><cell>9</cell></row>
</matrix>

Notes

  • Positions are 1-based (not 0-based), consistent with XPath sequence indexing.
  • Out-of-bounds access raises err:FOAY0001; use array:size() to guard.
  • The shorthand $array($pos) is syntactic sugar for array:get($array, $pos).

See also