[j-nsp] How to trim the text node of a specific element node in SLAX node-set?

Martin T m4rtntns at gmail.com
Wed Oct 24 06:14:04 EDT 2018


Hi!

I need to trim the text node of a specific element node in SLAX. All
other elements should remain as in the original node-set. At the
moment, I do it like this:

$ cat trim_node-set.slax
version  1.2;

main <top> {

  var $alphabet := {
    <letters> {
      <consonants> {
        <b pos="2"> "bbb  ";
        <c pos="3"> "ccc  ";
      }
      <consonants> {
        <d pos="4"> "ddd  ";
        <f pos="5"> "fff  ";
      }
    }
  }

  var $alphabet_trimmed := {
    <letters> {
      for-each ( $alphabet/letters/consonants ) {
        <consonants> {
          for-each ( * ) {
            if ( name() == "d" ) {
              <d pos="4"> translate(.," ", "");
            }
            else {
              copy-of .;
            }
          }
        }
      }
    }
  }

  copy-of $alphabet_trimmed;
}
$

As seen above, if element node is <d>, then space characters are
removed. Output of this script can be seen below:

$ slaxproc -g -E trim_node-set.slax
<?xml version="1.0"?>
<top>
  <letters>
    <consonants>
      <b pos="2">bbb  </b>
      <c pos="3">ccc  </c>
    </consonants>
    <consonants>
      <d pos="4">ddd</d>
      <f pos="5">fff  </f>
    </consonants>
  </letters>
</top>
$

Is there a more elegant way to do this? In addition, if I do the same
for rpc-reply, then for some reason, every element node gets the
'xmlns:junos="http://xml.juniper.net/junos/*/junos"' attribute node.
Just out of curiosity, why does this happen? As seen in the output
above, this does not happen in slaxproc, i.e <b>, <c> and <f> do not
get any attribute nodes added.


thanks,
Martin


More information about the juniper-nsp mailing list