Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 

key

 

Syntax

Release Information

Statement introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.

Description

Define a key for use with the key() XPath function. Keys are an alternative to IDs and are used to index the nodes within an XML document. The key must be defined as a top-level statement in the script. A key definition consists of the key identifier, the nodes to index, and the value that is paired with the key name to reference the matching nodes. The key() function is then used to locate the appropriate nodes.

The key() function works with the XML document of the current node and uses the specified key definition to retrieve nodes that are referenced by a particular name and value. The function arguments are the key name and the desired key’s value. The return value is a node set that includes all nodes referenced by that key name and value. If the desired key value is provided as a node set, rather than a string, the returned node set is a union of all the referenced nodes for the key values expressed by the nodes within the node set.

For example, if you define the key:

the following code would select <prototype> elements that have a name attribute with a value of "trace", and then output the value of the child element <return-type>:

Attributes

key nameKey identifier, which uniquely identifies the key within the script and is passed as the first argument to the key() function.
match patternXPath expression that selects the set of nodes to index.
value expressionXPath expression that defines the value of the key.

SLAX Example

The following op script creates two key definitions, protocol and next-hop, which are used to retrieve and display all static routes and all routes with a next hop of ge-0/0/0.0 on a device. The script invokes the Junos XML API get-route-information command to obtain the route information for the device. The for-each( $results ) statement changes the current node to the $results XML document. The subsequent for-each loops use the keys to retrieve all nodes that are indexed according to the key names and values.

The for-each( key( "protocol", "Static" ) statement uses the protocol key definition, which matches on route-table/rt elements, to retrieve the desired nodes. The rt-entry/protocol-name key value matches the <protocol-name> child elements that have the value "Static". The code block executes using <rt> as the context node. For each match, the script outputs the value of the <rt-destination> element.

The for-each( key( "next-hop", "ge-0/0/0.0" ) statement uses the "next-hop" key definition, which matches on route-table/rt elements, to retrieve the desired nodes. The rt-entry/nh/via key value matches the <via> child elements that have the value "ge-0/0/0.0". The code block executes using <rt> as the context node. For each match, the script outputs the value of the <rt-destination> element.