Download This Guide
Supported Platforms
Related Documentation
- ACX, EX, M, MX, SRX, T Series
- Example: Displaying DNS Hostname Information Using an Op Script
- Junos Script Automation: Understanding Extension Functions in the jcs and slax Namespaces
- ACX, EX, M, MX, PTX, SRX, T Series
- Summary of Extension Functions in the jcs and slax Namespaces
- empty() Function (jcs and slax Namespaces)
first-of() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
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.
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: Displaying DNS Hostname Information Using an Op Script.
Related Documentation
- ACX, EX, M, MX, SRX, T Series
- Example: Displaying DNS Hostname Information Using an Op Script
- Junos Script Automation: Understanding Extension Functions in the jcs and slax Namespaces
- ACX, EX, M, MX, PTX, SRX, T Series
- Summary of Extension Functions in the jcs and slax Namespaces
- empty() Function (jcs and slax Namespaces)
Published: 2013-03-05
Download This Guide
Supported Platforms
Related Documentation
- ACX, EX, M, MX, SRX, T Series
- Example: Displaying DNS Hostname Information Using an Op Script
- Junos Script Automation: Understanding Extension Functions in the jcs and slax Namespaces
- ACX, EX, M, MX, PTX, SRX, T Series
- Summary of Extension Functions in the jcs and slax Namespaces
- empty() Function (jcs and slax Namespaces)