Name
[2.0] zero-or-one()
Raises an error unless its argument is a sequence
containing zero or one items. For example, if a <name>
element can have at most
one <title>
child,
zero-or-one(title)
would raise
an error if a <name>
element had the wrong number of <title>
elements. Be aware that
zero-or-one()
terminates
processing; for a more flexible approach, use the count()
function to determine the cardinality of a
sequence.
Syntax
item()?zero-or-one(
item()*
)
Inputs
A sequence of items. The sequence must either be empty or a singleton.
Outputs
This function returns the input sequence, assuming it has
zero or one items. If the input sequence has more than one item,
the zero-or-one()
function
raises an error.
Defined in
XQuery 1.0 and XPath 2.0 Functions and Operators section 15.2, “Functions That Test the Cardinality of Sequences.” More details about this function can be found in XQuery 1.0 and XPath 2.0 Formal Semantics section 7.2, “Standard Functions with Specific Static Typing Rules.”
Example
We’ll look at a trivial example that illustrates the
zero-or-one()
function.
Invoking zero-or-one()
with
a sequence containing more than one item raises an error. Here’s
the stylesheet:
<?xml version="1.0"?>
<!-- zero-or-one.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:variable name="emptySequence" as="item()*"> <xsl:sequence select="()"/> </xsl:variable> <xsl:variable ...
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.