简体   繁体   English

两个XML文档之间的差异ruby on rails

[英]Difference between two XML documents ruby on rails

是否有可能在ruby中的两个XML文档中确定差异(例如,新元素,已修改元素,已删除元素...)?

May I quote Aaron Patterson: 我可以引用亚伦帕特森的话:

As far as the XML document is concerned, no two nodes are ever equal. 就XML文档而言,没有两个节点是平等的。 Every node in a document is different. 文档中的每个节点都是不同的。 Every node has many attributes to compare: 每个节点都有许多要比较的属性:

  1. Is the name the same? 这个名字是一样的吗?
  2. How about attributes? 属性怎么样?
  3. How about the namespace? 命名空间怎么样?
  4. What about number of children? 孩子的数量怎么样?
  5. Are all the children the same? 所有的孩子都一样吗?
  6. Is it's parent node the same? 它的父节点是否相同?
  7. What about it's position relative to sibling nodes? 它相对于兄弟节点的位置怎么样?

Think about adding two nodes to the same document. 考虑将两个节点添加到同一文档中。 They can never have the same position relative to sibling nodes, therefore two nodes in a document cannot be "equal". 它们永远不会相对于兄弟节点具有相同的位置,因此文档中的两个节点不能“相等”。

You can however compare two different documents. 但是,您可以比较两个不同的文档。 But you need to answer those 7 questions yourself as you're walking the two trees. 但是当你走在两棵树上时,你需要自己回答这7个问题。 Your requirements for sameness may differ from others. 您对同一性的要求可能与其他要求不同。

I wouldn't be opposed to implementing a =~ on Node that did this comparison, but was very strict about those questions. 我不反对在节点上实现一个=〜做这个比较,但对这些问题非常严格。

You could do stuff like: 你可以这样做:

doc1 =~ doc2 # => true doc1 = ~doc2#=> true
doc2 =~ doc3 # => false doc2 = ~doc3#=> false

As long as it only returned true or false. 只要它只返回真或假。 How does that sound? 听起来怎么样?

-- Aaron Patterson http://tenderlovemaking.com/ - Aaron Patterson http://tenderlovemaking.com/


Otherwise, there are lookarounds : 否则, 有一些看法

load the XML doc into a hash with Hash#from_xml and compare the hashes. 使用Hash#from_xml将XML文档加载到哈希中并比较哈希值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM