home contribute faq download

FunctX XQuery Functions

functx:if-empty

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

Google
Webdatypic.com

Description

The functx:if-empty function is useful for providing default values in case an element or attribute is empty or non-existant. An element is considered empty if it does not have any content (e.g. <value></value> or <value/>). An attribute is considered empty if its value is a zero-length string (e.g. value=""). If $node is not empty, this function returns the typed value of the node. If it is empty, or it is non-existent (equal to the empty sequence), it returns the value $value.

Note: This function is similar to the eg:if-empty 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 node that may be empty
$value item()* the item(s) to use if the node is empty
return value item()*

XQuery Function Declaration

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

  if (string($arg) != '')
  then data($arg)
  else $value
 } ;

Examples

let $in-xml :=
<prices>
   <price discount="10.00">29.99</price>
   <price discount="6.00">39.99</price>
   <price></price>
   <price discount="">49.99</price>
</prices>
return
XQuery ExampleResults
functx:if-empty($in-xml//price[1], 0)
29.99
functx:if-empty($in-xml//price[3], 0)
0
functx:if-empty($in-xml//price[99], 0)
0
functx:if-empty($in-xml//price[1]/@discount, 0)
10.00
functx:if-empty($in-xml//price[3]/@discount, 0)
0
functx:if-empty($in-xml//price[4]/@discount, 0)
0

See Also

functx:if-absentThe first argument if it is not empty, 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-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, https://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery