简体   繁体   中英

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. 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. 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
doc2 =~ doc3 # => false

As long as it only returned true or false. How does that sound?

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


Otherwise, there are lookarounds :

load the XML doc into a hash with Hash#from_xml and compare the hashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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