Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Announcement: Try the Ask AI chatbot for answers to your technical questions about Juniper products and solutions.

close
header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Automation Scripting User Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

position()

date_range 13-Feb-21

Syntax

content_copy zoom_out_map
number position()

Description

Return the current context position among the list of nodes that are currently being evaluated. The context position is the index of the node within the node-set being evaluated by a predicate, or if position() is being used outside of a predicate, then it is the index of the current node within the current node list. The initial position is 1 and the final position is equal to the context size, which can be retrieved through the last() function.

Usage Examples

The following op script shows the effect of using the position() function in both location path predicates as well as within for-each loops.

content_copy zoom_out_map
version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

match / {
    var $host-name-set := {
        <host-name> "PE1";
        <host-name> "P1";
        <host-name> "P2";
        <host-name> "PE2";
    }

    var $first-host-name = $host-name-set/host-name[ position() == 1 ];
    expr jcs:output( "First host-name: ", $first-host-name );

    var $first-p-host-name = $host-name-set/host-name[not(starts-with(.,"PE"))][position() == 1];

    expr jcs:output( "First P host-name: ", $first-p-host-name );

    expr jcs:output( "All host-names:" );
    for-each( $host-name-set/host-name ) {
        expr jcs:output( position(), ": ", . );
    }

    expr jcs:output( "P host-names only:" );
    for-each( $host-name-set/host-name[ not(starts-with( ., "PE" ))] ) {
        expr jcs:output( position(), ": ", . );
    }
}
content_copy zoom_out_map
user@host> op position
First host-name: PE1
First P host-name: P1
All host-names:
1: PE1
2: P1
3: P2
4: PE2
P host-names only:
1: P1
2: P2
footer-navigation