Sponsored

XSLT Reference

map:remove()

XSLT 3.0 map function

Returns a new map with one or more specified keys removed, leaving the original map unchanged.

Syntax
map:remove(map, keys)

Description

map:remove() produces a new map that contains all entries from the input map except those whose keys appear in the keys sequence. If a key in keys is not present in the map, it is silently ignored. Maps are immutable; the original is not modified.

Parameters

ParameterTypeRequiredDescription
mapmap(*)YesThe source map.
keysxs:anyAtomicType*YesA sequence of keys to remove.

Return value

map(*) — a new map with the specified keys removed.

Examples

Removing a single key

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:map="http://www.w3.org/2005/xpath-functions/map">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="user" select="map{
      'name': 'Alice',
      'email': 'alice@example.com',
      'password': 'secret123'
    }"/>
    <!-- Remove sensitive field before output -->
    <xsl:variable name="safe" select="map:remove($user, 'password')"/>
    <user>
      <xsl:for-each select="sort(map:keys($safe))">
        <xsl:element name="{.}"><xsl:value-of select="map:get($safe, .)"/></xsl:element>
      </xsl:for-each>
    </user>
  </xsl:template>
</xsl:stylesheet>

Output:

<user>
  <email>alice@example.com</email>
  <name>Alice</name>
</user>

Removing multiple keys at once

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:map="http://www.w3.org/2005/xpath-functions/map">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="record" select="map{
      'id':1, 'name':'Bob', 'ssn':'999-99-9999',
      'dob':'1990-01-01', 'city':'London'
    }"/>
    <xsl:variable name="private-keys" select="('ssn', 'dob')"/>
    <xsl:variable name="public" select="map:remove($record, $private-keys)"/>
    <public-record size="{map:size($public)}">
      <xsl:for-each select="sort(map:keys($public))">
        <field name="{.}"><xsl:value-of select="map:get($public, .)"/></field>
      </xsl:for-each>
    </public-record>
  </xsl:template>
</xsl:stylesheet>

Output:

<public-record size="3">
  <field name="city">London</field>
  <field name="id">1</field>
  <field name="name">Bob</field>
</public-record>

Notes

  • Removing a non-existent key is not an error; it is silently ignored.
  • Maps are immutable; map:remove() always returns a new map.
  • To remove all keys, use map:remove($m, map:keys($m)), which returns an empty map.

See also