Python ElementTree不喜欢在处理指令的名称中使用冒号

2024-05-19 01:08:56 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码:

import xml.etree.ElementTree as ET

xml = '''\
<?xml version="1.0" encoding="UTF-8"?>
<testCaseConfig>
    <?LazyComment Blah de blah/?>   
    <testCase runLimit="420" name="d1/n1"/>
    <testCase runLimit="420" name="d1/n2"/>
</testCaseConfig>'''

root = ET.fromstring(xml)

xml2 = xml.replace('LazyComment ', 'LazyComment:')
print(xml2)
try:
    root2 = ET.fromstring(xml2)
except ET.ParseError:
    print("\nERROR in xml2!!!\n")

xml3 = xml2.replace('testCaseConfig', 'testCaseConfig xmlns:Blah="http://www.w3.org/TR/html4/"', 1)
print(xml3)
try:
    root3 = ET.fromstring(xml3)
except ET.ParseError:
    print("\nERROR in xml3!!!\n")
    raise

给出以下输出:

^{pr2}$

我搜索并找到了指向我阅读的其他资源的this Q。在

看来“?”使其成为标记名可以包含冒号的处理指令。没有“?”然后名称中的冒号表示名称空间,其中一个答案指出,定义名称空间可以使事情正常工作。在

组合用“?”但是“:”会导致ElementTree出现问题。在

我得到了这种类型的xml文件,其他工具可以使用这些文件进行解析,并希望自己使用Python处理这些文件。有什么想法吗?在

谢谢。在


Tags: 文件name名称xmltestcaseetblahprint
2条回答

根据Common Syntactic Constructs下的W3C可扩展标记语言1.0规范:

The Namespaces in XML Recommendation [XML Names] assigns a meaning to names containing colon characters. Therefore, authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character.

在W3C XPath 1.0中关于Processing Instruction nodes的说明:

A processing instruction has an expanded-name: the local part is the processing instruction's target; the namespace URI is null.

总之,<?LazyComment:Blah de blah/?>是一条无效的处理指令,因为冒号用于引用命名空间uri,并用于处理部分为null或空的指令。因此,Python的XML处理器抱怨使用这样的指令并不能呈现格式良好的XML。在

另外,请重新考虑那些生成无效处理指令的工具,因为它们没有处理有效的XML文档。这类工具可能将XML文件视为文本文档(类似于您能够替换XML的字符串表示,但无法使用etree附加指令的方式)。在

<?xml version="1.0" encoding="UTF-8"?>
<testCaseConfig xmlns:Blah="http://www.w3.org/TR/html4/">
    <?LazyComment:Blah de blah/?>   
    <testCase runLimit="420" name="d1/n1"/>
    <testCase runLimit="420" name="d1/n2"/>
</testCaseConfig xmlns:Blah="http://www.w3.org/TR/html4/">

是无效的XML。结束标记中不能有属性。最后一行应该是</testCaseConfig>

也可以这样写评论

^{pr2}$

相关问题 更多 >

    热门问题