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 TypeName | Type | Description |
$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 DeclarationSee 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> |
XPath Example | 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 |
See AlsoHistory |
Recommended Reading:
|