Discussion:
[j-nsp] How to trim the text node of a specific element node in SLAX node-set?
Martin T
2018-10-24 10:14:04 UTC
Permalink
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
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
2018-10-25 19:09:29 UTC
Permalink
Post by Martin T
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
Use apply-templates and a distinct mode to traverse the entire
hierarchy and apply rules to rebuild content, like:

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 $new = {
apply-templates $alphabet {
mode "trim";
}
}

<new> { copy-of $new; }
}

match text() {
mode "trim";

if (name(..) == "d") {
<d pos="4"> 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-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Martin T
2018-11-02 14:08:40 UTC
Permalink
Post by Phil Shafer
Post by Martin T
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
Use apply-templates and a distinct mode to traverse the entire
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 $new = {
apply-templates $alphabet {
mode "trim";
}
}
<new> { copy-of $new; }
}
match text() {
mode "trim";
if (name(..) == "d") {
<d pos="4"> translate(., " ", "");
} else {
copy-of .;
}
}
mode "trim";
copy-node {
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-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
2018-11-02 19:08:34 UTC
Permalink
Post by Martin T
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-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp

Loading...