Pavel Rodionov
Pavel Rodionov

Reputation: 380

JavaScript implementation of XML differences

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:

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>

Upvotes: 2

Views: 650

Answers (1)

Andy E
Andy E

Reputation: 344685

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.

Upvotes: 0

Related Questions