蓝色天空.知识 · 实践 · 创造
我的收藏
XML DOM 教程目录 40章
XML DOM / LEARN BY DOING

DOM DocumentImpl

讲解独立于具体文档实例的 DOMImplementation 接口定位,以及 createDocument、createDocumentType、getFeature 与 hasFeature 四项核心方法。

学完这一章,你能够

  • 讲解独立于具体文档实例的 DOMImplementation 接口定位,以及 createDocument、createDocumentType、getFeature 与 hasFeature 四项核心方法。

DOMImplementation 接口定位

`DOMImplementation`(早期中文资料亦写作 `DocumentImplementation`)封装了一组独立于任何特定 `Document` 实例的操作,最核心的职责是在不依赖既有文档的前提下创建全新的 `DocumentType` 节点与 XML `Document` 对象,通常通过 `document.implementation` 或 Python 的 `xml.dom.minidom.getDOMImplementation()` 获取。

DOMImplementation 对象方法

该接口在 W3C DOM Level 2/3 中定义了四项方法:`createDocument()` 与 `createDocumentType()` 用于从零构造 XML 文档与 DTD 声明;`hasFeature()` 用于探测特性支持(需注意在现代 DOM Living Standard 中因历史兼容性问题已被规定恒返回 `true`);`getFeature()` 则是 DOM Level 3 扩展,现已在现代浏览器中移除。

DOMImplementation 对象方法参考

方法参数与作用说明
createDocument(namespaceURI, qualifiedName, doctype)创建一个带有指定根元素命名空间、限定名以及可选 `DocumentType` 的全新 XML `Document` 对象。
createDocumentType(qualifiedName, publicId, systemId)创建一个空的 `DocumentType` 声明节点(可随后传入 `createDocument()` 作为新文档的 DOCTYPE)。
getFeature(feature, version)历史 DOM Level 3 方法:返回实现指定特性与版本的专用对象,不支持时返回 `null`(现代 DOM 已移除)。
hasFeature(feature, version)检查 DOM 实现是否支持指定特性和版本(在 `minidom` 中按特性表真实判定;在现代浏览器 DOM 标准中已固定返回 `true`)。

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

含完整源码 · 109 行
展开查看收起
python
"""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()
离线验证器通过全部 3 项用例:使用 getDOMImplementation() 构造带命名空间与 DocumentType 的新文档,实测 hasFeature 对 Core/XML/未知特性的返回值,并验证接口与具体文档的独立性。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a2756.zip) ↓ 查看运行说明 ↗

本章附件与实例包

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

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

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

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

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

因为新文档本身尚不存在,无法依赖现有文档内部的节点工厂方法;`DOMImplementation` 提供了独立于单个文档实例的顶层构造入口,专门用于引导生成新的 `DocumentType` 和 `Document`。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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