Junos Automation (Scripting)
Reply
Contributor
JoshTX
Posts: 65
Registered: ‎09-14-2009
0
Accepted Solution

JUNOSCRIPT/Slax: how to use a variable in a node

I'm trying to get the value of a set policer depending on what encapsulation is set on a unit.  Since family inet has not encapsulation set, I've used the if clause below to set family to 'inet' if no encapsulation is set.  This works well.

 

        var $family = {
           if ($top/interfaces/interface[name == $phyifname]/unit[name == $unit]/encapsulation) {
             expr $top/interfaces/interface[name == $phyifname]/unit[name == $unit]/encapsulation;
           } else {
             expr "inet";
           }
        }
        var $policer_in = $top/interfaces/interface[name == $phyifname]/unit[name == $unit]/family[name == $family]/policer/input;
        var $policer_out = $top/interfaces/interface[name == $phyifname]/unit[name == $unit]/family[name == $family]/policer/output;

The problem comes in the last two lines.  The 'family[name == $family]' doesn't work as I'd expect.  I believe this is because family xxx is its own node (whats the right term for this?), as opposed to a node that expects an argument (ie, name in slax)

 

 

Can someone set me in the right direction on this?

Recognized Expert
ccall
Posts: 237
Registered: ‎06-18-2008

Re: JUNOSCRIPT/Slax: how to use a variable in a node

You can check the name of the context node by using the name() function.

 

So something like this might be what you are looking for:

 

var $policer_in = $top/interfaces/interface[name == $phyifname]/unit[name == $unit]/family/*[name() == $family]/policer/input;

Contributor
JoshTX
Posts: 65
Registered: ‎09-14-2009
0

Re: JUNOSCRIPT/Slax: how to use a variable in a node

That worked nicely.  What is the * for before [name()... ?

 

Recognized Expert
ccall
Posts: 237
Registered: ‎06-18-2008
0

Re: JUNOSCRIPT/Slax: how to use a variable in a node

* is the wildcard that matches all nodes for the given axis.  The default axis is child, so the * in that location path matches all child nodes of <family> and then the predicate [ ] narrows them down to only child nodes with the same name as the $family variable.

Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.