3

I need this for patching. I can't change the first xml, but I have the possibility to apply .xslt to create patched version from original file.

Which commands I should use?

Also, which command will create the second .xml from first xml and xslt ?

i.e. I want something like MS XMLDiffPatch (~year 2002), but with XSLT instead of Diffgram (Xml Diff Language).

I expected that such work is already done for XSLT. Because for RFC5261 (https://tools.ietf.org/html/rfc5261) it was done in 2012 (3 years ago) - http://github.com/Bonuspunkt/XmlPatch
Any version of XSLT is suitable (i.e. 2.0, 3.0).

Some theoretical works in this field:

Linked questions: - Track changes to XML file in XSLT format - How to correctly diff trees (that is, nested lists of strings)? - Building an HTML Diff/Patch Algorithm - Diff algorithms - tree / diff-algorithm - Detect differences between tree structures - Diff Algorithm - Semantic Diff Utilities - XML Diff and Merge

Community
  • 1
  • 1
user1709408
  • 518
  • 4
  • 16
  • 1
    Too board. You have to define the schema of the first and second file, then write the XSLT to do the transformation. And there's no way to "restore" the second file back to the first. You will have to write another XSLT for the task. – Code Different Sep 18 '15 at 16:15
  • I don't need to transform second file back to the first. I want an utility which will create .xslt for direct transform from first to second only. – user1709408 Sep 18 '15 at 16:16
  • @ZoffDino, there may exist such a thing as lossless transformation, in which case restoration is possible, but indeed, will require another XSLT to do it. – Abel Sep 18 '15 at 16:48
  • Take a look at the DeltaXML product. – Michael Kay Sep 19 '15 at 14:09
  • It is not said, that DeltaXML generates XSLT as diff - http://www.deltaxml.com/products/merge/ – user1709408 Sep 19 '15 at 14:17
  • Afaik this question has even a theoretical importance, I would suggest to reopen it (and try to make it looking better). – peterh Sep 19 '15 at 16:07
  • 1
    Dear @user1709408 , Abel's answer is correct, but maybe if you are looking for some interesting algorithm, for example to minimize the changes or similar, then http://cs.stackexchange.com waits you. – peterh Sep 19 '15 at 16:08

1 Answers1

4

If I have 2 .xml files, how to create .xslt which will transform first file into second file?

This can be done with:

<!-- two XML files -->
<xsl:param name="file1" select="'file1.xml'" />
<xsl:param name="file2" select="'file2.xml'" />

<!-- call with -it main on commandline, will transform file1 into file2 -->
<xsl:template name="main">
    <xsl:result-document href="{$file2}" >
        <xsl:copy-of select="document($file1)" />
    </xsl:result-document>
</xsl:template>

Use xsl:apply-templates and a copy idiom if you need to do some transformation on the first into the second file.

Be aware that the you cannot read and write to the same file. So if you want to also read from file2.xml then you should use a different URI for writing.

Which commands I should use? Any version of XSLT is suitable (i.e. 2.0, 3.0).

On the tag's info page there's a listing of a myriad of processors that support from XSLT 1.0 to 3.0. You can choose whichever one you like, though the above example will work with XSLT 2.0 and up only.

Community
  • 1
  • 1
Abel
  • 52,738
  • 19
  • 137
  • 227
  • it doesn't transform first file into second one, because it doesn't contain differences inside .xslt. It just perform raw copying. – user1709408 Sep 18 '15 at 16:42
  • I can choose any processor to apply .xslt (which is second step). – user1709408 Sep 18 '15 at 16:44
  • 2
    @user1709408, yes, so it transforms the first file into second file. Without any requirements, that's the only thing I could make understand from your question. But, as a hint, I just updated the question and suggested to use `xsl:apply-templates`, the rest is the same. – Abel Sep 18 '15 at 16:44
  • But I want an utility which records differences into .xslt automatically (like diff) – user1709408 Sep 18 '15 at 16:44
  • @user1709408, difference of XSLT? You mean, you have an XSLT that does a transformation and now you want to use the XSLT as input to record how a new transformation should look like based on a perceived new A->B change? Something like automatic reverse engineering? That sounds to me like a research question, but for sure, I didn't read that in your original question... – Abel Sep 18 '15 at 16:46
  • No, I want something like MS XMLDiffPatch, but with XSLT instead of Diffgram (Xml Diff Language) – user1709408 Sep 18 '15 at 16:48
  • @user1709408, so, the XSLT has to be auto-generated based on the differences of the two files? Still a research project, your first step should be to learn all there is to know about XSLT, XPath, XDM and related technologies, as it is possible, but will require a lot of work. – Abel Sep 18 '15 at 16:50
  • I expected that such work is already done for XSLT. Because for RFC5261 it was done 3 years ago - https://github.com/Bonuspunkt/XmlPatch – user1709408 Sep 18 '15 at 16:53
  • 2
    @user1709408, slowly you are getting closer to what you actually want. Why don't you rewrite your question so that it explains what you actually want? I don't know about expectations, but implementation of a standard relies on people needing it. If there was no need, then nobody builds it. But feel free to do so. I may be wrong, perhaps it exists, but I haven't heard of it. – Abel Sep 18 '15 at 16:57