xml - 从 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."
解决方案
你只需要这样:
$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
推荐阅读
- spring-boot - 模拟方法总是返回空 Optional
- airflow - 从 Airflow REST Experimental API 读取配置/数据
- python - 无法将 ESP8266 连接到 Flask 服务器
- scheme - 如何在 Racket 中获取此代码以仅返回非重复项目?
- spring - org.hibernate.AnnotationException:xxx.UserPDO.jobTitleDetail 上的@OneToOne 或@ManyToOne 引用了一个未知实体:xxx.JobTitleData
- php - 适用于所有子类别的 WP 一个模板
- java - E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-3 Callable 返回了一个空值
- deep-learning - 什么是断棒变量?
- image - Vue 表单提交显示带有标题的奇怪红色下拉列表
- c# - 使用双缓冲技术进行并发读写?