first-of() Function (SLAX and XSLT)
Namespaces
http://xml.juniper.net/junos/commit-scripts/1.0 http://xml.libslax.org/slax
SLAX Syntax
var $result = prefix:first-of(object, "expression");
XSLT Syntax
<xsl:variable name="result" select="prefix:first-of(object, 'expression')"/>
Description
Return the first nonempty (non-null) item in a
list. If all objects in the list are empty, the default expression
is returned. This function provides the same functionality as an if / else-if / else
construct but
in a much more concise format.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
expression |
Default value returned if all objects in the list are empty. |
object |
List of objects. |
Return Value
result |
First nonempty (non-null) item in the object list. If all objects in the list are empty, the default expression is returned. |
Usage Examples
In the following example, if the value of $a
is empty, $b
is checked.
If the value of $b
is empty, $c
is checked. If the value of $c
is empty, $d
is checked. If the value
of $d
is empty, the string "none" is returned.
jcs:first-of($a, $b, $c, $d, "none")
In the following example, for each physical interface, the script checks for a description of each logical interface. If a logical interface description does not exist, the function returns the description of the (parent) physical interface. If the parent physical interface description does not exist, the function returns a message that no description was found.
var $rpc = <get-interface-information>; var $results = jcs:invoke($rpc); for-each ($results/physical-interface/logical-interface) { var $description = jcs:first-of(description, ../description, "no description found"); }
The equivalent XSLT code is:
<xsl:variable name="rpc"> <get-interface-information/> </xsl:variable> <xsl:variable name="results" select="jcs:invoke($rpc)"/> <xsl:for-each select="$results/physical-interface/logical-interface"> <xsl:variable name="description" select="jcs:first-of(description, ../description, 'no description found')"/> </xsl:for-each>
The code for the description
variable declaration in the previous examples would be equivalent
to the following more verbose if / else-if / else
construct:
var $description = { if (description) { expr description; } else if (../description) { expr ../description; } else { expr "no description found"; } }
See also Example: Display DNS Hostname Information Using an Op Script.
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.