Name

[2.0] exists()

Returns true if the input sequence is nonempty; false otherwise.

Syntax

xs:boolean exists(item()*)

Input

A sequence of items.

Output

If the sequence of items is not empty, exists() returns true; otherwise, it returns false.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 15.1, “General Functions and Operators on Sequences.”

Example

This simple stylesheet uses the exists() function against three sequences. The first sequence is the empty sequence, the second is a singleton, and the third is a longer sequence:

<?xml version="1.0"?>
<!-- exists.xsl -->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:output method="text"/>

  <xsl:template match="/">

    <xsl:text>&#xA;Here is a test of the exists() </xsl:text>
    <xsl:text>function:&#xA;&#xA;  </xsl:text>

    <xsl:variable name="emptySequence" as="item()*">
      <xsl:sequence select="()"/>
    </xsl:variable>

    <xsl:variable name="singleton" as="item()*">
      <xsl:sequence select="(3)"/>
    </xsl:variable>

    <xsl:variable name="longSequence" as="item()*">
      <xsl:sequence 
        select="(3, 4, 5, current-date(), current-time(), 8, 'blue',
                'red', xs:float(3.14), 42, xs:date('1995-04-21'))"/>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="exists($emptySequence)">
        <xsl:text>The empty sequence does exist!</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>The empty sequence doesn't exist!</xsl:text>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:text>&#xA;  </xsl:text>
 <xsl:value-of select="if ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.