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