"""Offline XML DOM verification lab for Chapter 12 (mozilla-vs-ie)."""
import json
import sys
import xml.dom.minidom as minidom
from lxml import etree
VERSION = f"CPython {sys.version.split()[0]} + xml.dom.minidom + lxml {etree.__version__}"
BOOKS_XML = """<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>"""
BOOK_CRLF_XML = (
"<book>\r\n"
" <title>Everyday Italian</title>\r\n"
" <author>Giada De Laurentiis</author>\r\n"
" <year>2005</year>\r\n"
" <price>30.00</price>\r\n"
"</book>"
)
def case_w3c_preserves_whitespace_text_nodes():
doc = minidom.parseString(BOOKS_XML)
children = doc.documentElement.childNodes
node_types = [n.nodeType for n in children]
expected = {
"total_child_nodes": 9,
"element_nodes": 4,
"whitespace_text_nodes": 5,
"type_sequence": [3, 1, 3, 1, 3, 1, 3, 1, 3],
}
actual = {
"total_child_nodes": len(children),
"element_nodes": sum(1 for t in node_types if t == 1),
"whitespace_text_nodes": sum(1 for t in node_types if t == 3),
"type_sequence": node_types,
}
return {
"name": "w3c_preserves_whitespace_text_nodes",
"summary": "验证遵循 W3C DOM 规范的解析器保留缩进与换行空白文本节点,4 本书的 bookstore 共有 9 个 childNodes",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_legacy_ie_vs_element_only_count():
doc = minidom.parseString(BOOKS_XML)
elem_only = [n for n in doc.documentElement.childNodes if n.nodeType == 1]
parser = etree.XMLParser(remove_blank_text=True)
lxml_root = etree.fromstring(BOOKS_XML.encode("utf-8"), parser=parser)
expected = {
"element_only_count": 4,
"lxml_stripped_child_count": 4,
"matches_legacy_ie_and_modern_children": True,
}
actual = {
"element_only_count": len(elem_only),
"lxml_stripped_child_count": len(lxml_root),
"matches_legacy_ie_and_modern_children": len(elem_only) == len(lxml_root) == 4,
}
return {
"name": "legacy_ie_vs_element_only_count",
"summary": "对比按 nodeType==1 过滤及 lxml remove_blank_text=True 的结果(均为 4),对应旧版 IE 与现代 element.children",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_indented_book_crlf_whitespace_nodes():
doc = minidom.parseString(BOOK_CRLF_XML)
children = doc.documentElement.childNodes
elem_names = [n.tagName for n in children if n.nodeType == 1]
expected = {
"raw_child_nodes": 9,
"element_children": ["title", "author", "year", "price"],
"first_child_is_text_node": True,
}
actual = {
"raw_child_nodes": len(children),
"element_children": elem_names,
"first_child_is_text_node": children[0].nodeType == 3,
}
return {
"name": "indented_book_crlf_whitespace_nodes",
"summary": "解析含 CRLF 换行与两空格缩进的 book 示例,验证其原始子节点数为 9 而元素子节点为 4",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_compact_xml_zero_whitespace_nodes():
compact = "<bookstore><book/><book/><book/><book/></bookstore>"
doc = minidom.parseString(compact)
expected = {"child_count": 4, "all_elements": True}
actual = {
"child_count": len(doc.documentElement.childNodes),
"all_elements": all(n.nodeType == 1 for n in doc.documentElement.childNodes),
}
return {
"name": "compact_xml_zero_whitespace_nodes",
"summary": "验证紧凑无空白 XML 在 W3C DOM 解析器中不产生空白文本节点,childNodes 数量直接等于 4",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_w3c_preserves_whitespace_text_nodes(),
case_legacy_ie_vs_element_only_count(),
case_indented_book_crlf_whitespace_nodes(),
case_compact_xml_zero_whitespace_nodes(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。
入口文件:projects/dom-mozilla-vs-ie-lab/explore-mozilla-vs-ie.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mozilla-vs-ie.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14(xml.dom.minidom + lxml 6.1.3)离线 XML DOM 验证环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a2752.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2766.zip