another SO question为基础,如何检查两个格式正确的XML代码段在语义上是否相等。我需要的只是“相等”与否,因为我正在将此用于单元测试。

在我想要的系统中,这些值相等(请注意“开始”的顺序
和“结束”):

<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<Stats start="1275955200" end="1276041599">
</Stats>

# Reordered start and end

<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<Stats end="1276041599" start="1275955200" >
</Stats>

我可以使用lmxl和其他工具,并且一个仅允许对属性进行重新排序的简单函数也可以正常工作!

基于IanB答案的工作片段:
from formencode.doctest_xml_compare import xml_compare
# have to strip these or fromstring carps
xml1 = """    <?xml version='1.0' encoding='utf-8' standalone='yes'?>
    <Stats start="1275955200" end="1276041599"></Stats>"""
xml2 = """     <?xml version='1.0' encoding='utf-8' standalone='yes'?>
    <Stats end="1276041599" start="1275955200"></Stats>"""
xml3 = """ <?xml version='1.0' encoding='utf-8' standalone='yes'?>
    <Stats start="1275955200"></Stats>"""

from lxml import etree
tree1 = etree.fromstring(xml1.strip())
tree2 = etree.fromstring(xml2.strip())
tree3 = etree.fromstring(xml3.strip())

import sys
reporter = lambda x: sys.stdout.write(x + "\n")

assert xml_compare(tree1,tree2,reporter)
assert xml_compare(tree1,tree3,reporter) is False

最佳答案

您可以使用formencode.doctest_xml_compare-xml_compare函数比较两个ElementTree或lxml树。

07-27 22:32