2

Is there some javaScript implementation to handle deltas between XML data?

The main point is to detect the existence of the difference, it doesn't matter what was changed: attribute or node value.

Requirements are as follows:

  • Each node will have unique id (it's one of the simplifications made to find more candidates-libraries)
  • Deltas should be checked in nodes, attributes and node values
  • Support XML node hierarchies up to 3 levels
  • The result of computation should be also XML (see example), but it could be 3 arrays of added, updated and deleted nodes
  • Ignore some subnodes in delta calculation, for example I want to track just 3 levels of hierarchy, not more
  • Changes detection should not be propagated to upper nodes, so for example child node changes should not make parent node updated

Here is example how it should work:
XML#1:

<node id="0">
  <node id="1">
     <node id="4">
       <node id="23">DATA</data>
     </node>
     <node id="5">DATA</node>
  </node>  
</node>

XML#2:

<node id="0">
  <node id="1">
     <node id="3">
        <node id="342">DATA</data>
     </node>
     <node id="5" some_attribute="attr"/>
  </node>  
  <node id="6"/>
</node >

So result should be the following:

<result>
   <added>
      <id>6</id>
      <id>3</id> 
      <id>342</id> 
   </added>
   <updated>
      <id>5</id>
   </updated>
   <removed>
      <id>4</id>
      <id>23</id>
   </removed>
</result>
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
Pavel Rodionov
  • 380
  • 1
  • 2
  • 8
  • Whilst this is going to be strictly possible in JS, I doubt that it will be easy, efficient, or reliable. Can you tell us why you need to do this in JS rather than the more obvious XSLT? – annakata Jun 16 '09 at 16:08
  • Maybe XSLT will be useful in some situtations, but it requires server side changes for us, which is inappropriate. My use case is that we are using some heavy-weight client side component, and full re-rendering of such component is slow. This component loads particular XML when it was changed, but it should rerender only changed parts,not fully. I am thinking about transparent Http-proxy component some kind of "delta engine", but it's a question from different context. I will start new question, if here I'll have no luck. – Pavel Rodionov Jun 17 '09 at 16:22

1 Answers1

0

I'm not a big user of XSLT, but I'm sure you could use it to achieve what you're trying to do if you're familiar with it. Maybe you should try adding the tag to the question.

The following XSLT document, taken from http://msdn.microsoft.com/en-us/magazine/cc164169.aspx, will supposedly merge two documents, but I doubt it would be as thorough as you want it to:

<xsl:transform version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <Container>
      <xsl:copy-of select="document('product.xml')"/>
      <xsl:copy-of select="document('material.xml')"/>        
    </Container>
  </xsl:template>
</xsl:stylesheet>

The examples on that page are for .NET, but Internet Explorer has access to many MSXML ActiveX controls that might provide the functions you need if cross-browser compatibility isn't a requirement.

Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • I'd +1 if the OP was ok with XSLT, merges being a fairly common and googleable use-case for XSLT. – annakata Jun 16 '09 at 16:05
  • True. Another way of doing it would be to loop through each node and attribute in XMLFile1 and then run a XMLFile2.selectSingleNode() passing the XPath expression of the node or attribute to check if it exists, then using an === on the nodevalues to see if they are different, and write the result to a third XML file as described above. I only wish I had the time to write it :-) – Andy E Jun 16 '09 at 16:27
  • Sorry but cross-browser compatibility is strong requirement. Server side development for XSLT templates is out of context. – Pavel Rodionov Jun 17 '09 at 16:24