[j-nsp] initialize multiple mutable variables in SLAX with the same value

Phil Shafer phil at juniper.net
Thu Mar 8 00:47:12 EST 2018


Martin T writes:
>I would like to initialize multiple mutable variables with the same
>value. I could initialize variables one by one like this:

There's no sort of run-time variable naming like this in SLAX.
Variable names are just plain tokens (qnames).

If your data is an array, consider using a single variable
that contains a set of values, like:

    var $x := {
        <item> "one";
        <item> "two";
        <item> "three";
    }

so $x[1] is "one" (position() is 1-origin).  This allows
building data like:

    var $x := {
        for-each (some/other/thing) {
            <item> .;
        }
    }

But this makes updating a single array member difficult.

The root of this is that XSLT (and SLAX) use a very different
programming model than traditional languages, one that lends itself
to recursion and immutable variables.  In general, you are better
off following that model.

That said, sometimes it's easier to thing of a problem in traditional
patterns.  For more background on mutable variables into SLAX:

    http://juniper.github.io/libslax/slax-manual.html#mutable-variables-2

Thanks,
 Phil


More information about the juniper-nsp mailing list