ON THIS PAGE
position()
Syntax
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.
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(), ": ", . ); } }
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