问题:
当空格不重要时,表示可能非常重要。
说明:
在 XML 模式第 2 部分:数据类型第二版中,约束方面 whiteSpace 是为派生自 string (http://www.w3.org/TR/xmlschema-2/#rf-whiteSpace) 的类型定义的。如果此 whiteSpace facet 是replace 或collapse,则该值可能会在 normalization 期间更改.
Section 4.3.6末尾有注释:
The notation #xA used here (and elsewhere in this specification) represents the Universal Character Set (UCS) code point hexadecimal A (line feed), which is denoted by U+000A. This notation is to be distinguished from 
, which is the XML character reference to that same UCS code point.
示例:
如果元素elem 的数据类型具有空白 约束collapse,"<elem> text </elem>"
应该变成 "text"
(删除了前导和尾随空格),但是 "<elem> text </elem>"
应该变成 " text "
(未删除由字符引用编码的空格)。
问题:
因此要么解析器/树构建器处理此规范化,要么在之后完成。
- 知情解析:
- 我在哪里向解析器或树构建器提供有关如何规范化某些 XML 元素的信息?
- 有没有类似
set_whitespace_normalization('./country/neighbor', 'collapse')
的东西? - 有没有像
normalize(content)
这样的钩子(Hook)在解析器或树生成器中?
- 后处理
- 如何访问某些元素的原始内容?
- 有没有
elem.original_text
, 那可能会返回 " text 
"? - 有没有
elem.unnormalized_text
,这可能会返回“text
”?
我想使用 Python 的 xml.etree.ElementTree,但我会考虑执行此任务的任何其他 XML 库。
免责声明:
当然,声明空白无关紧要(replace 或 collapse)然后使用字符引用作弊是一种糟糕的风格。在大多数情况下,应该更改数据或模式以防止发生这种情况,但有时您必须使用外部 XML 模式和外部 XML 文档。上面引用的注释的存在表明 XML 编辑器意识到了这种困境并且没有故意阻止它。
最佳答案
这似乎是 xml.etree.ElementTree 中的一个已知错误:http://bugs.python.org/issue17582 .根据该错误报告,这在 lxml.etree 中得到了正确处理:https://pypi.python.org/pypi/lxml/ .
关于python - 我如何使用 Python 的元素树处理空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16974950/