Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Diff: How to generate XML diff using XSLT?

Tags:

I would like to compute the diff between two XML files or nodes using XSL/XSLT. Is there any stylesheet readily available or any simple way of doing it?

like image 959
Vincent Avatar asked Nov 20 '09 15:11

Vincent


People also ask

How use XSLT is XML?

XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation . The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology perspective.

What is difference between XML and XSLT?

XSLT (eXtensible Stylesheet Language Transformations) is the recommended style sheet language for XML. XSLT is far more sophisticated than CSS. With XSLT you can add/remove elements and attributes to or from the output file.

What is current () XSLT?

XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.


2 Answers

Interesting question! I once tried to do something similar involving two XML sources, and my experience was that there just ain't no way.

You could use XSL's facility for including user-built functions, and code up something really slick. But I really can't see it.

If I were to do something like this, I'd process the two XML files in parallel using DOM4J, which lets me easily traverse the code programmatically and do detail sub-queries.

Trying to do this in XSLT will either prove you to be a genius or drive you into madness.

like image 147
Carl Smotricz Avatar answered Oct 28 '22 13:10

Carl Smotricz


XSLT is data-driven, that is, it goes through the single source XML file top to bottom looking for template matches in the XSL stylesheet. The templates don't really know where they are in the data, they just run their code when matched. You can reference another XML source, but the program will run according to the traversal of the original source.

So when you arrive at the nth child element of <blarg>, for example, you could look up the nth child of <blarg> in a second XML using the document() function. But the usefulness of this depends on the structure of your XML and what comparisons you're trying to do.

This behavior is opposite of most traditional scripts, which run through the program code top to bottom, calling on the data file when instructed. The latter--pull processing--is what you probably need to compare two XML sources. XSLT will break down in comparison as soon as there is a difference.

like image 36
carillonator Avatar answered Oct 28 '22 13:10

carillonator