RE: [xsl] Is it possible to modify an XML by XSLT "in-place" (directly replace) ?

Subject: RE: [xsl] Is it possible to modify an XML by XSLT "in-place" (directly replace) ?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 8 Oct 2009 22:08:30 +0100
> Normally the user applys an XSLT style sheet onto a source 
> XML file or variable and the result XML will be stored into 
> ANOTHER file or variable.
> Thats the "traditional" way of processing XSLT.
> 
> But what, if I want to replace the original file or variable 
> completely?
> Ok, I could do this after the actual XSLT processing in 
> another step by copying the result file/variable back to the 
> source and delete the temporary help file/variable.

I would not recommend writing the output of a transformation to the same
file as the input unless you have a backup of the input. It is likely that
if the transformation fails for any reason, the input file will be lost. The
XSLT 2.0 specification explicitly states that when a transformation fails,
the state of any persistent resources updated by the transformation is
undefined.

I spent many years working on an operating system with versioned filestore
(ICL VME) that did not have this problem: if a failure occurred you could
always revert to the previous version of the file. Unfortunately "modern"
operating systems are generally less sophisticated than this. 

The usual procedure therefore is to write to a file with a new name, and
then rename the file (to the original name) if the process completes
successfully.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 

Current Thread