loris has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Monks,

I am using XML::LibXML to delete all the child nodes of a certain type below the parent node while retaining the other child nodes, apart from empty text nodes. For reasons unknown to me, all the information stored is as attributes and the text nodes are all always empty.

The problem is that for each deleted node, a text node is left behind. So if I start with:

<table> <header colname1="monk" colname2="favourite syllable"> <row monk="mainmonk" syllable="om"/> <row monk="loris" syllable="erm"/> </table>

and remove all the rows with

my @rowNodes = $parent->findnodes("row"); foreach my $rowNode (@rowNodes) { $parent->removeChild($rowNode); }

I end up with

<table> <header colname1="monk" colname2="favourite syllable"> </table>

rather than

<table> <header colname1="monk" colname2="favourite syllable"> </table>

Can anyone tell me how to remove the empty text nodes corresponding to the row nodes?

Thanks,

loris

Replies are listed 'Best First'.
Re: XML::LibXML - Removing text nodes
by Mutant (Priest) on Nov 10, 2004 at 10:29 UTC
    I think you need to turn off the 'keep blanks' mode:
    $parser->keep_blanks(0);
Re: XML::LibXML - Removing text nodes
by Anonymous Monk on Nov 10, 2004 at 11:11 UTC
    s/\n{2,}/\n/g;

    Don't use the API, work around it ;-)

    kludgemeister

      Or, when I have finished twiddling,

      xmllint --format ugly.xml > pretty.xml

      But it goes a bit against the grain ...

      loris