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

离线验证 Attr 归属关系、命名空间属性变更、DTD 默认值与 ID 属性判定

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

VERSION = "CPython 3.12.14 offline XML DOM verifier (xml.dom.minidom + lxml.etree)"

BOOK_XML = '<book xml:base="https://example.org/lib/" xmlns:pub="urn:pub" category="web" pub:isbn="978-0-123456-47-2"><title>XML DOM</title></book>'


def case_attr_core_and_owner_properties():
    doc = minidom.parseString(BOOK_XML)
    book = doc.documentElement
    attr = book.getAttributeNode("category")

    expected = {
        "name": "category",
        "value": "web",
        "nodeName": "category",
        "nodeValue": "web",
        "nodeType": 2,
        "ownerElement_is_book": True,
        "ownerDocument_is_doc": True,
        "parentNode_is_none": True,
        "not_in_childNodes": True,
    }
    actual = {
        "name": attr.name,
        "value": attr.value,
        "nodeName": attr.nodeName,
        "nodeValue": attr.nodeValue,
        "nodeType": attr.nodeType,
        "ownerElement_is_book": attr.ownerElement is book,
        "ownerDocument_is_doc": attr.ownerDocument is doc,
        "parentNode_is_none": attr.parentNode is None,
        "not_in_childNodes": all(c.nodeType != Node.ATTRIBUTE_NODE for c in book.childNodes),
    }
    return {
        "name": "attr_core_and_owner_properties",
        "summary": "验证 Attr 节点核心属性、ownerElement/ownerDocument 归属及属性非元素子节点的规范特征",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_attr_namespace_and_value_mutation():
    doc = minidom.parseString(BOOK_XML)
    book = doc.documentElement
    ns_attr = book.getAttributeNodeNS("urn:pub", "isbn")
    before_val = ns_attr.value
    ns_attr.value = "978-0-999999-00-1"

    expected = {
        "namespaceURI": "urn:pub",
        "prefix": "pub",
        "localName": "isbn",
        "before_val": "978-0-123456-47-2",
        "after_value": "978-0-999999-00-1",
        "after_nodeValue": "978-0-999999-00-1",
        "reflected_on_element": "978-0-999999-00-1",
    }
    actual = {
        "namespaceURI": ns_attr.namespaceURI,
        "prefix": ns_attr.prefix,
        "localName": ns_attr.localName,
        "before_val": before_val,
        "after_value": ns_attr.value,
        "after_nodeValue": ns_attr.nodeValue,
        "reflected_on_element": book.getAttributeNS("urn:pub", "isbn"),
    }
    return {
        "name": "attr_namespace_and_value_mutation",
        "summary": "验证带命名空间 Attr 的 namespaceURI/prefix/localName 及修改 value 同步更新 nodeValue 与元素属性",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_attr_specified_isid_and_schema_info():
    doc = minidom.parseString(BOOK_XML)
    book = doc.documentElement
    cat = book.getAttributeNode("category")
    before_is_id = cat.isId
    book.setIdAttributeNode(cat)
    after_is_id = cat.isId

    # Observe DTD default attribute injection vs explicit document attribute via lxml.etree
    dtd_xml = b"""<!DOCTYPE item [
      <!ELEMENT item EMPTY>
      <!ATTLIST item status CDATA "draft" code CDATA #REQUIRED>
    ]><item code="X1"/>"""
    dtd_root = etree.fromstring(dtd_xml, parser=etree.XMLParser(load_dtd=True, attribute_defaults=True))
    explicit_in_source = "code" in dtd_root.attrib and 'status=' not in dtd_xml.decode("utf-8")
    default_from_dtd = dtd_root.attrib.get("status") == "draft"

    lroot = etree.fromstring(BOOK_XML.encode("utf-8"))
    expected = {
        "has_specified_attr_on_minidom": True,
        "explicit_code_vs_dtd_default_status": True,
        "before_isId": False,
        "after_isId": True,
        "lookup_by_id_matches_book": True,
        "owner_baseURI": "https://example.org/lib/",
        "schemaTypeInfo_without_xsd_is_none": True,
    }
    actual = {
        "has_specified_attr_on_minidom": isinstance(cat.specified, bool),
        "explicit_code_vs_dtd_default_status": explicit_in_source and default_from_dtd,
        "before_isId": before_is_id,
        "after_isId": after_is_id,
        "lookup_by_id_matches_book": doc.getElementById("web") is book,
        "owner_baseURI": lroot.base,
        "schemaTypeInfo_without_xsd_is_none": getattr(cat, "schemaType", None) in (None, (None, None)) or getattr(cat.schemaType, "name", None) is None,
    }
    return {
        "name": "attr_specified_isid_and_schema_info",
        "summary": "验证 Attr.specified、setIdAttributeNode 切换 isId、baseURI 继承与无模式校验时的 schemaTypeInfo",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_attr_core_and_owner_properties(),
        case_attr_namespace_and_value_mutation(),
        case_attr_specified_isid_and_schema_info(),
    ]
    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-attribute-lab/explore-attribute.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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