home contribute faq download

FunctX XSLT Functions

functx:if-empty

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

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

XSLT Function Declaration

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

  <xsl:sequence select="
  if (string($arg) != '')
  then data($arg)
  else $value
 "/>

</xsl:function>

Examples

<xsl:variable name="in-xml" as="item()*">
<prices>
   <price discount="10.00">29.99</price>
   <price discount="6.00">39.99</price>
   <price></price>
   <price discount="">49.99</price>
</prices>
</xsl:variable>
XSLT ExampleResults
<functx:if-empty($in-xml>unctx:if-empty($in-xml//price[1], 0)
<29.99>9.99
<functx:if-empty($in-xml>unctx:if-empty($in-xml//price[3], 0)
<0>
<functx:if-empty($in-xml>unctx:if-empty($in-xml//price[99], 0)
<0>
<functx:if-empty($in-xml>unctx:if-empty($in-xml//price[1]/@discount, 0)
<10.00>0.00
<functx:if-empty($in-xml>unctx:if-empty($in-xml//price[3]/@discount, 0)
<0>
<functx:if-empty($in-xml>unctx: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, http://www.datypic.com
Datypic XSLT Services

Recommended Reading:

XQuery