Context: .NET Framework 3.5
I realize how I can perform a single XML transformation using XSLT, but didn't find any good examples on chaining XML transformations.
Input: - XML document as XPathDocument. - File paths to multiple XSL files.
Expected output: - preferably XPathDocument/IXPathNavigable, representing the XML with all transformations applied, one by one.
Example scenario:
input xml: <doc></doc>
xsl-1: .xsl that adds <one /> as a child of the doc element.
xsl-2: .xsl that adds <two /> as a child of the doc element.
Expected result
<doc><one /><two /></doc>
Goals
Leverage the forward only nature of XPathDocument/IXPathNavigable or better. Avoid loading entire document in memory.
Maybe something like the following (I have not tried to compile this):
XslCompiledTransform xsl1 = new XslCompiledTransform();
xsl1.Load("xsl1.xsl");
XslCompiledTransform xsl2 = new XslCompiledTransform();
xsl1.Load("xsl2.xsl");
using (Stream stream = new MemoryStream())
{
using (XmlReader xmlReader1 = XmlReader.Create("source.xml"))
{
xsl1.Transform(xmlReader1, stream);
}
stream1.Position = 0;
using (XmlReader xmlReader2 = XmlReader.Create(stream))
{
xsl2.Transform(xmlReader2, "output.xml");
}
}
By using the xmlreader you will get the forward only you are looking for. I have just outpu the first result to a MemoryStream but you could do this to a temporary file.
For that extra little bit of performance you may want to look at pre compiling your xslt.
XSLT Compiler (xsltc.exe)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With