Sponsored

XSLT Reference

array:remove()

XSLT 3.0 array function

Returns a new array with the members at the specified 1-based positions removed.

Syntax
array:remove(array, positions)

Description

array:remove() returns a new array with specified members omitted. The positions argument is a sequence of 1-based integers identifying the members to remove. Positions may be supplied in any order; duplicates are ignored. Members not listed in positions are retained in their original relative order.

If positions is the empty sequence, the function returns a copy of the input array unchanged. All specified positions must be valid (between 1 and array:size(array) inclusive); an out-of-range position raises a dynamic error.

Parameters

ParameterTypeRequiredDescription
arrayarray(*)YesThe source array.
positionsxs:integer*YesA sequence of 1-based positions to remove.

Return value

array(*) — a new array with the specified members removed, preserving the relative order of remaining members.

Examples

Removing a single member

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="text"/>

  <xsl:template match="/">
    <xsl:variable name="a" select="['A', 'B', 'C', 'D']"/>
    <xsl:variable name="result" select="array:remove($a, 2)"/>
    <xsl:value-of select="array:flatten($result)" separator=" "/>
  </xsl:template>
</xsl:stylesheet>

Output:

A C D

Removing multiple members

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<data/>

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="/data">
    <xsl:variable name="a" select="[10, 20, 30, 40, 50]"/>
    <!-- Remove positions 1 and 3 -->
    <xsl:variable name="result" select="array:remove($a, (1, 3))"/>
    <remaining>
      <xsl:for-each select="1 to array:size($result)">
        <item><xsl:value-of select="array:get($result, .)"/></item>
      </xsl:for-each>
    </remaining>
  </xsl:template>
</xsl:stylesheet>

Output:

<remaining>
  <item>20</item>
  <item>40</item>
  <item>50</item>
</remaining>

Notes

  • array:remove() removes by position, not by value. To remove by value, combine array:filter() with a value comparison.
  • Positions are 1-based, consistent with all other array functions.
  • Duplicate positions in the positions sequence are silently ignored.
  • Removing all positions results in an empty array []; removing no positions (()) returns a copy of the input.

See also