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

Martin T m4rtntns at gmail.com
Fri Nov 2 10:08:40 EDT 2018


On Thu, Oct 25, 2018 at 10:08 PM Phil Shafer <phil at juniper.net> 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 <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

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


More information about the juniper-nsp mailing list