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

2018-11-02 Thread Phil Shafer
Martin T writes:
>Thanks Phil! I wasn't aware of the mode statement. However, as I don't
>see a performance benefit, then solution in my initial e-mail looks
>bit simpler.

It always depends on the rules and the structure of the contents
you are converting, but when someone says "I want to traverse this
hierarchy looking for ...", then apply-templates/mode is normally
a pretty good answer.

Thanks,
 Phil
___
juniper-nsp mailing list juniper-nsp@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp


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

2018-11-02 Thread Martin T
On Thu, Oct 25, 2018 at 10:08 PM Phil Shafer  wrote:
>
> Martin T writes:
> >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:
>
> Use apply-templates and a distinct mode to traverse the entire
> hierarchy and apply rules to rebuild content, like:
>
> version 1.2;
>
> main  {
> var $alphabet :=  {
>  {
>  "bbb  ";
>  "ccc  ";
> }
>  {
>  "ddd  ";
>  "fff  ";
> }
> }
>
> var $new = {
> apply-templates $alphabet {
> mode "trim";
> }
> }
>
>  { copy-of $new; }
> }
>
> match text() {
> mode "trim";
>
> if (name(..) == "d") {
>  translate(., " ", "");
> } else {
> copy-of .;
> }
> }
>
> match @* | * | processing-instruction() | comment() {
> mode "trim";
>
> copy-node {
> apply-templates * |@* | text() | processing-instruction() | comment() 
> {
> mode "trim";
> }
> }
> }
>
> Thanks,
>  Phil

Thanks Phil! I wasn't aware of the mode statement. However, as I don't
see a performance benefit, then solution in my initial e-mail looks
bit simpler.


Martin
___
juniper-nsp mailing list juniper-nsp@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp


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

2018-10-25 Thread Phil Shafer
Martin T writes:
>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:

Use apply-templates and a distinct mode to traverse the entire
hierarchy and apply rules to rebuild content, like:

version 1.2;

main  {
var $alphabet :=  {
 {
 "bbb  ";
 "ccc  ";
}
 {
 "ddd  ";
 "fff  ";
}
}

var $new = {
apply-templates $alphabet {
mode "trim";
}
}

 { copy-of $new; }
}

match text() {
mode "trim";

if (name(..) == "d") {
 translate(., " ", "");
} else {
copy-of .;
}
}

match @* | * | processing-instruction() | comment() {
mode "trim";

copy-node {
apply-templates * |@* | text() | processing-instruction() | comment() {
mode "trim";
}
}
}

Thanks,
 Phil
___
juniper-nsp mailing list juniper-nsp@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp


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

2018-10-24 Thread Martin T
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  {

  var $alphabet := {
 {
   {
 "bbb  ";
 "ccc  ";
  }
   {
 "ddd  ";
 "fff  ";
  }
}
  }

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

  copy-of $alphabet_trimmed;
}
$

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

$ slaxproc -g -E trim_node-set.slax


  

  bbb  
  ccc  


  ddd
  fff  

  

$

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 ,  and  do not
get any attribute nodes added.


thanks,
Martin
___
juniper-nsp mailing list juniper-nsp@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp