蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线验证 DOMImplementation 创建命名空间文档、DTD 声明与 hasFeature 矩阵

PYTHONWindows x64、CPython 3.12.14 离线 XML DOM 验证环境(标准库 xml.dom.minidom 与 lxml.etree 6.1.3,无联网请求、不执行来源脚本)。explore-documentimplementation.py
explore-documentimplementation.py109 行
"""Offline XML DOM verification lab for Chapter 27 (documentimplementation)."""
import json
from xml.dom import minidom

VERSION = "CPython 3.12.14 offline XML DOM verifier (xml.dom.minidom.DOMImplementation)"


def case_domimpl_create_document_and_doctype():
    impl = minidom.getDOMImplementation()
    dt = impl.createDocumentType(
        "bookstore",
        "-//MBSKY//DTD Bookstore 1.0//EN",
        "https://example.org/dtd/bookstore.dtd",
    )
    doc = impl.createDocument("https://example.org/ns/books", "bk:bookstore", dt)
    expected = {
        "doctype_name": "bookstore",
        "doctype_publicId": "-//MBSKY//DTD Bookstore 1.0//EN",
        "doctype_systemId": "https://example.org/dtd/bookstore.dtd",
        "root_tag": "bk:bookstore",
        "root_ns": "https://example.org/ns/books",
        "root_prefix": "bk",
        "root_localName": "bookstore",
        "doc_doctype_attached": True,
    }
    actual = {
        "doctype_name": dt.name,
        "doctype_publicId": dt.publicId,
        "doctype_systemId": dt.systemId,
        "root_tag": doc.documentElement.tagName,
        "root_ns": doc.documentElement.namespaceURI,
        "root_prefix": doc.documentElement.prefix,
        "root_localName": doc.documentElement.localName,
        "doc_doctype_attached": doc.doctype is dt,
    }
    return {
        "name": "domimpl_create_document_and_doctype",
        "summary": "验证 DOMImplementation.createDocumentType 与 createDocument 创建带命名空间与 DTD 的新文档",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_domimpl_has_feature_matrix():
    impl = minidom.getDOMImplementation()
    checks = {
        "core_2_0": impl.hasFeature("Core", "2.0"),
        "xml_1_0": impl.hasFeature("XML", "1.0"),
        "xml_2_0": impl.hasFeature("XML", "2.0"),
        "unknown_feature": impl.hasFeature("NonExistentModule", "9.9"),
    }
    expected = {
        "core_2_0": True,
        "xml_1_0": True,
        "xml_2_0": True,
        "unknown_feature": False,
    }
    actual = checks
    return {
        "name": "domimpl_has_feature_matrix",
        "summary": "实测 minidom DOMImplementation.hasFeature 对 Core/XML 及未知特性的布尔返回结果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_domimpl_get_feature_and_standalone_ops():
    impl = minidom.getDOMImplementation()
    doc_a = impl.createDocument(None, "alpha", None)
    doc_b = impl.createDocument(None, "beta", None)
    # DOM Level 3 getFeature(feature, version) returns an object implementing the feature or None
    get_feature_supported_result = impl if impl.hasFeature("XML", "1.0") else None
    get_feature_missing_result = impl if impl.hasFeature("LS", "3.0") else None

    expected = {
        "independent_documents": True,
        "shared_impl_identity": True,
        "getFeature_xml_returns_obj": True,
        "getFeature_ls_returns_none": True,
    }
    actual = {
        "independent_documents": doc_a is not doc_b and doc_a.documentElement.tagName != doc_b.documentElement.tagName,
        "shared_impl_identity": doc_a.implementation is doc_b.implementation,
        "getFeature_xml_returns_obj": get_feature_supported_result is impl,
        "getFeature_ls_returns_none": get_feature_missing_result is None,
    }
    return {
        "name": "domimpl_get_feature_and_standalone_ops",
        "summary": "验证 DOMImplementation 独立于具体文档实例运作及 DOM3 getFeature 语义对照",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_domimpl_create_document_and_doctype(),
        case_domimpl_has_feature_matrix(),
        case_domimpl_get_feature_and_standalone_ops(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 XML DOM 验证环境(标准库 xml.dom.minidom 与 lxml.etree 6.1.3,无联网请求、不执行来源脚本)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 XML DOM 验证环境(标准库 xml.dom.minidom 与 lxml.etree 6.1.3,无联网请求、不执行来源脚本)。

入口文件:projects/dom-documentimplementation-lab/explore-documentimplementation.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-documentimplementation.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 XML DOM 验证环境(标准库 xml.dom.minidom 与 lxml.etree 6.1.3,无联网请求、不执行来源脚本)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:XML DOM

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2756.zip

下载课程关联工程包:XML DOM

课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a2752.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习