home contribute faq download

FunctX XQuery Functions

functx:if-absent

The first argument if it is not empty, otherwise the second argument

Google
Webxqueryfunctions.com

Description

The functx:if-absent function is useful for providing default values in case a data item is absent. It checks if the $arg argument is the empty sequence, and if it is, it returns the value $value. Otherwise, it returns the typed value of $arg itself. Note that an element or attribute is not considered "absent" if it contains a zero-length string; only if it does not exist at all.

Note: This function is similar to the eg:if-absent function provided in the XQuery 1.0 and XPath 2.0 Functions and Operators document. However, this function differs in that it supports attribute nodes and atomic values.

Arguments and Return Type

NameTypeDescription
$arg item()* the item(s) that may be absent
$value item()* the item(s) to use if the item is absent
return value item()*

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:if-absent
  ( $arg as item()* ,
    $value as item()* )  as item()* {

    if (exists($arg))
    then $arg
    else $value
 } ;

Examples

let $in-xml :=
<prices>
   <price value="29.99" discount="10.00"/>
   <price value="39.99" discount="6.00"/>
   <price value="69.99"/>
   <price value="49.99" discount=""/>
</prices>
return
XQuery ExampleResults
data(functx:if-absent(
     $in-xml//price[1]/@discount, 0))
10.00
data(functx:if-absent(
     $in-xml//price[3]/@discount, 0))
0
data(functx:if-absent(
     $in-xml//price[4]/@discount, 0))
zero-length untyped value

See Also

functx:if-emptyThe first argument if it is not blank, otherwise the second argument
functx:all-whitespaceWhether a value is all whitespace or a zero-length string
fn:emptyWhether a value is the empty sequence

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C XML Query WG, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XQuery Services

Recommended Reading:

XQuery