如何从 Perl 中的字符串中去除无效的 XML 字符? [英] How can I strip invalid XML characters from strings in Perl?

查看:56
本文介绍了如何从 Perl 中的字符串中去除无效的 XML 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在将无效字符写入 XML 文件之前从字符串中去除无效字符的标准、批准和健壮的方法.我在这里谈论的是包含退格 (^H) 和换页符等的文本块.

I'm looking for what the standard, approved, and robust way of stripping invalid characters from strings before writing them to an XML file. I'm talking here about blocks of text containing backspace (^H) and formfeed characters etc.

必须有一个标准库/模块函数来执行此操作,但我找不到.

There has to be a standard library/module function for doing this but I can't find it.

我正在使用 XML::LibXML 来构建一个 DOM 树,然后我序列化到磁盘.

I'm using XML::LibXML to build a DOM tree that I then serialize to disk.

推荐答案

用于去除无效 xml-1.0 字符的完整正则表达式为:

The complete regex for removal of invalid xml-1.0 characters is:

# #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
$str =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;

对于 xml-1.1,它是:

for xml-1.1 it is:

# allowed: [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
$str =~ s/[^\x01-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
# restricted:[#x1-#x8][#xB-#xC][#xE-#x1F][#x7F-#x84][#x86-#x9F]
$str =~    s/[\x01-\x08\x0B-\x0C\x0E-\x1F\x7F-\x84\x86-\x9F]//go;

这篇关于如何从 Perl 中的字符串中去除无效的 XML 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆