[j-nsp] Conditional statement in XPath predicates

Jonathan Looney jonlooney at gmail.com
Fri Mar 9 09:07:58 EST 2018


Actually, come to think of it, this (while untested) will probably work and
be much simpler:

param $interface = "undef";

for-each ($result/physical-interface[$interface == "undef" || name ==
$interface]) {

}

Jonathan

On Fri, Mar 9, 2018 at 8:45 AM Jonathan Looney <jonlooney at gmail.com> wrote:

> There may be a more elegant way to do this, but this seems like a place
> where user-defined SLAX functions (as opposed to templates) can help, since
> functions can be used in XPATH expressions.
>
> If I recall correctly, we didn’t cover this well in the book, and I’ll
> take the blame for that. They are useful and are a good tool to have in
> your automation toolkit for those times when they are the right tool for
> the job.
>
> However, they are covered here (among other places):
> https://forums.juniper.net/t5/Automation/Scripting-How-To-Custom-functions-in-SLAX-scripts/ta-p/279074
> http://libslax.readthedocs.io/en/latest/constructs.html#index-18
>
> (Phil will probably be along soon to correct/confirm/supplement this
> information.)
>
> However, let me also say that you should generally always prefer to filter
> as close to the source as possible. For example, let’s say that you were
> trying to filter routes from a table with 1.4M routes. Let’s further say
> that each route’s XML information was 512 bytes (which I actually suspect
> might be a tad conservative). That is 700MB of data that RPD has to
> generate and cscript needs to consume. If you only want a subset of those,
> it would be much better to send an appropriate filter to RPD so it only
> gives you the routes you want. In that case, it would probably even be
> worth running a few extra cheaper RPCs (if needed) to construct the filter.
>
> Jonathan
>
> On Fri, Mar 9, 2018 at 7:42 AM Martin T <m4rtntns at gmail.com> wrote:
>
>> Hi,
>>
>> let's say that I have a SLAX script which takes an argument named
>> "interface". This "interface" can be an IFD. Is it possible to create
>> an XPath statement for for-each loop, which runs for each interface if
>> the "interface" is not specified and only for this one specific IFD,
>> if "interface" is specified? My first idea was to do something like
>> this:
>>
>> param $interface = "*";
>>
>> for-each ($result/physical-interface[name == $interface) {
>>
>> }
>>
>> This works only in case the "interface" argument is specified. When
>> the "interface" argument is not specified, then interfaces with the
>> literal name of * are searched and obviously, none are found. When I
>> define the "interface" parameter like this:
>>
>> param $interface = *;
>>
>> ..then nothing is printed because $interface seems to be an empty
>> node-set. As a next step, I tried with conditional statement:
>>
>> for-each ($result/physical-interface[name == (($interface) ? $interface :
>> *)]) {
>>
>> }
>>
>> ..or:
>>
>> for-each ($result/physical-interface[name == (($interface != "") ?
>> $interface : *)]) {
>>
>> }
>>
>> ..but those do not work either. On the other hand, "[name == *]" works
>> fine.
>>
>> Am I doing it wrong or such statements are not supported?
>>
>>
>> thanks,
>> Martin
>> _______________________________________________
>> juniper-nsp mailing list juniper-nsp at puck.nether.net
>> https://puck.nether.net/mailman/listinfo/juniper-nsp
>>
>


More information about the juniper-nsp mailing list