functx:if-empty
The first argument if it is not blank, otherwise the second argument
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.
Parameters |
Description |
---|---|
arg:item() |
the node that may be empty |
value:item() |
the item(s) to use if the node is empty |
Examples
XPath |
Results |
---|---|
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 |