比较XML代码段? [英] Compare XML snippets?

查看:78
本文介绍了比较XML代码段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个SO问题为基础,如何检查两个格式正确的XML代码段在语义上是相等的。我需要的只是是否等于,因为我正在将其用于单元测试。



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

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

#重新排序了开始和结束

<?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 
#必须剥离这些或fromstring鲤鱼
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 ='是'?>
< Stats end = 1276041599 start = 1275955200>< / Stats>
xml3 =<?xml version ='1.0'encoding ='utf-8'standalone ='yes'?>
< Stats start = 1275955200>< / Stats>来自lxml的

导入etree
tree1 = etree.fromstring(xml1.strip())
tree2 = etree.fromstring(xml2.strip())
tree3 = etree.fromstring(xml3.strip())

导入系统
记者= lambda x:sys.stdout.write(x + \n)

断言xml_compare(tree1,tree2,reporter)
断言xml_compare(tree1,tree3,reporter)为F其他


解决方案

您可以使用 formencode.doctest_xml_compare - xmlTree或Trees比较两个元素。 p>

Building on another SO question, how can one check whether two well-formed XML snippets are semantically equal. All I need is "equal" or not, since I'm using this for unit tests.

In the system I want, these would be equal (note the order of 'start' and 'end'):

<?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>

I have lmxl and other tools at my disposal, and a simple function that only allows reordering of attributes would work fine as well!


Working snippet based on IanB's answer:

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

解决方案

You can use formencode.doctest_xml_compare -- the xml_compare function compares two ElementTree or lxml trees.

这篇关于比较XML代码段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆