[j-nsp] break statement in SLAX

Martin T m4rtntns at gmail.com
Thu Dec 13 12:15:43 EST 2018


Hi!

I have a XML text node which contains random integers between 1 and 10
and are separated by LF(0x0a). I convert this text node into a string
and use jcs:break-lines() to put those numbers into a node-set. This
means, that eventually I have an object like this:

(sdb) print $int_ns
[node-set] (10)
<text>8</text>
<text>8</text>
<text>2</text>
<text>1</text>
<text>2</text>
<text>9</text>
<text>3</text>
<text>4</text>
<text>7</text>
<text>1</text>

(sdb)


Now I want to process only the first three integers which are less
than 5. As I understand, for-each loop does not support breaking out
from the loop so I did it like this:

$ cat read_part_of_node-set.slax
version  1.2;

main <top> {

        var $int := {
                <text> 8;
                <text> 8;
                <text> 2;
                <text> 1;
                <text> 2;
                <text> 9;
                <text> 3;
                <text> 4;
                <text> 7;
                <text> 1;
        }

        var $int_ns := $int/*;

        mvar $n;
        set $n = 1;

        mvar $m;
        set $m = 1;

        while ( $m < 4 ) {
                if ( $int_ns[number($n)] < 5 )  {
                        <output> $int_ns[number($n)];
                        set $m = $m + 1;
                }
                set $n = $n + 1;
        }
}
$ slaxproc -g -E read_part_of_node-set.slax
<?xml version="1.0"?>
<top>
  <output>2</output>
  <output>1</output>
  <output>2</output>
</top>
$

Just wondering, maybe there is a more elegant solution for this?


thanks,
Martin


More information about the juniper-nsp mailing list