Multiline incoming: This is my 1st line. This is my 2nd line. This is my 3rd line. Is there a w" /> Multiline incoming: This is my 1st line. This is my 2nd line. This is my 3rd line. Is there a w" /> Multiline incoming: This is my 1st line. This is my 2nd line. This is my 3rd line. Is there a w"/>

Get rid of white spaces from multiline inside XML tag

136 Views Asked by At

I have this block of XML:

 <context id="134">Multiline incoming: 
    This is my 1st line. 
    This is my 2nd line. 
    This is my 3rd line. 
 </context> 

Is there a way to remove the spacing automatically with any XML parsing library like LibXML in Perl? I can get rid of them manually, but I am interested if there is a method for this already in place.

I'd like my output to be:

$context = "Multiline incoming: This is my 1st line. This is my 2nd line. This is my 3rd line." 
1

There are 1 best solutions below

0
ikegami On

All you need is this:

$context =~ s/^\s+//;          # Remove leading whitespace
$context =~ s/\s*\v\s*/ /g;    # Replace whitespace that includes vertical whitespace 
$context =~ s/\s+\z//;         # Remove trailing whitespace