position()
语法
number position()
描述
返回当前正在评估的节点列表中的当前上下文位置。上下文位置是谓词正在评估的节点集中节点的索引,或者如果在谓词外部使用,则 position()
它是当前节点列表中当前节点的索引。初始位置为 1,最终位置等于上下文大小,可以通过 last() 函数检索。
使用示例
以下操作脚本显示了在位置路径谓词以及 for-each 循环中使用该 position()
函数的效果。
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