首页 > 解决方案 > 从 XML 标记内的多行中删除空格

问题描述

我有这个 XML 块:

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

有没有办法使用任何 XML 解析库(如 Perl 中的 LibXML)自动删除间距?我可以手动摆脱它们,但如果有一种方法已经到位,我很感兴趣。

我希望我的输出是:

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

标签: xmlperlxml-libxml

解决方案


你只需要这样:

$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

推荐阅读