home contribute faq download

FunctX XSLT Functions

functx:if-absent

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

Google
Webxsltfunctions.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()*

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:if-absent" as="item()*"
              xmlns:functx="http://www.functx.com">
  <xsl:param name="arg" as="item()*"/>
  <xsl:param name="value" as="item()*"/>

  <xsl:sequence select="
    if (exists($arg))
    then $arg
    else $value
 "/>

</xsl:function>

Examples

<xsl:variable name="in-xml" as="item()*">
<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>
</xsl:variable>
XPath 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 XSLT Services

Recommended Reading:

XQuery