Sponsored

XSLT Reference

array:filter()

XSLT 3.0 array function

Returns a new array containing only the members for which a predicate function returns true.

Syntax
array:filter(array, predicate)

Description

array:filter() applies a predicate function to each member of the input array and returns a new array containing only the members for which the predicate returns true. The order of surviving members is preserved and the original array is not modified.

The predicate is an inline or named function with signature function(item()*) as xs:boolean. Each member of the array—whether it is a single item or a sequence—is passed to the predicate as a whole unit.

Parameters

ParameterTypeRequiredDescription
arrayarray(*)YesThe source array whose members are tested.
predicatefunction(item()*) as xs:booleanYesA function that returns true for members to keep.

Return value

array(*) — a new array containing only the members for which the predicate returned true. The size may be zero if no members pass.

Examples

Filtering numbers greater than 5

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:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:array="http://www.w3.org/2005/xpath-functions/array">

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

  <xsl:template match="/">
    <xsl:variable name="numbers" select="[1, 7, 3, 9, 2, 6, 4, 8]"/>
    <xsl:variable name="above5" select="array:filter($numbers, function($n) { $n gt 5 })"/>
    <result>
      <input-size><xsl:value-of select="array:size($numbers)"/></input-size>
      <output-size><xsl:value-of select="array:size($above5)"/></output-size>
      <xsl:for-each select="1 to array:size($above5)">
        <value><xsl:value-of select="array:get($above5, .)"/></value>
      </xsl:for-each>
    </result>
  </xsl:template>
</xsl:stylesheet>

Output:

<result>
  <input-size>8</input-size>
  <output-size>4</output-size>
  <value>7</value>
  <value>9</value>
  <value>6</value>
  <value>8</value>
</result>

Filtering non-empty strings from an XML source

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<tags>
  <tag>xslt</tag>
  <tag/>
  <tag>xpath</tag>
  <tag>  </tag>
  <tag>saxon</tag>
</tags>

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="/tags">
    <xsl:variable name="all" select="array:join(for $t in tag return [$t])"/>
    <xsl:variable name="filled" select="array:filter($all, function($t) { normalize-space($t) != '' })"/>
    <filtered count="{array:size($filled)}">
      <xsl:for-each select="1 to array:size($filled)">
        <tag><xsl:value-of select="array:get($filled, .)"/></tag>
      </xsl:for-each>
    </filtered>
  </xsl:template>
</xsl:stylesheet>

Output:

<filtered count="3">
  <tag>xslt</tag>
  <tag>xpath</tag>
  <tag>saxon</tag>
</filtered>

Notes

  • Each array member is passed as a whole unit to the predicate. A member that is itself a sequence is passed as that sequence, not as individual items.
  • array:filter() always returns a new array; the source array is unmodified.
  • To apply a transformation rather than a selection, use array:for-each().
  • If no member satisfies the predicate, an empty array [] is returned.

See also