0

I'm using C# to transform an XML document and it is working fine:

using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

public class XmlTransformUtil
{

public static void Main(string[] args)
{

    if (args.Length == 2)
    {

        Transform(args[0], args[1]);
    }
    else
    {

        PrintUsage();
    }
}

public static void Transform(string sXmlPath, string sXslPath)
{
    try
    {
        //load the Xml doc
        XPathDocument myXPathDoc = new XPathDocument(sXmlPath);

        XslTransform myXslTrans = new XslTransform();

        //load the Xsl 
        myXslTrans.Load(sXslPath);

        //create the output stream
        XmlTextWriter myWriter = new XmlTextWriter
            ("result.html", null);

        //do the actual transform of Xml
        myXslTrans.Transform(myXPathDoc, null, myWriter);

        myWriter.Close();
    }

    catch (Exception e)
    {
        Console.WriteLine("Exception: {0}", e.ToString());
    }
}

public static void PrintUsage()
{
    Console.WriteLine
    ("Usage: XmlTransformUtil.exe <xml path> <xsl path>");
}

}

The above code works perfectly, however what I want to do is, before the XSLT is transformed, I want it to add additional Lines of code to specific parts of the XSLT.

XSLT Code:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <html lang="en-GB">
        <body style="font-family:'Praxis Com Light'; color:#632423; width:100%; font-size:14px !important;">

//MAIN BODY OF CODE

        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

How i want the XSLT Code changed in C# Before the transformation:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="HEAD.xslt"/>
<xsl:include href="FOOT.xslt"/>

<xsl:template match="/">
    <html lang="en-GB">
        <body style="font-family:'Praxis Com Light'; color:#632423; width:100%; font-size:14px !important;">

        <xsl:call-template name="Header"/>

//MAIN BODY OF CODE

            <xsl:call-template name="Footer"/>

            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

How can this be accomplished?

Tim C
  • 70,053
  • 14
  • 74
  • 93
AaronParkes
  • 313
  • 5
  • 15
  • 2
    Look instead of passing in parameters to the transform, and key off of that. Example - http://stackoverflow.com/questions/1521064/passing-parameters-to-xslt-stylesheet-via-net – OldProgrammer Jul 10 '15 at 12:37
  • Not sure what the difficulty is. An XSLT stylesheet is an XML document, you have a tool (XSLT) for transforming XML documents, so just do it. Whether transforming the stylesheet is the right strategy here I don't know (it's a powerful technique, but there are sometimes other ways that are better), but intrinsically, if you want to use XSLT to transform XSLT, that's no problem. – Michael Kay Jul 10 '15 at 14:31

1 Answers1

1
XNamespace ns = "http://www.w3.org/1999/XSL/Transform";

XElement xslt = XElement.Load(sXslPath);

xslt.AddFirst(new XElement(ns + "include", new XAttribute("href", "FOOT.xslt")));
xslt.AddFirst(new XElement(ns + "include", new XAttribute("href", "HEAD.xslt")));

XElement body = xslt.Descendants("body").Single();

body.AddFirst(new XElement(ns + "call-template", new XAttribute("name", "Header")));
body.Add(new XElement(ns + "call-template", new XAttribute("name", "Footer")));
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • I think to have the newly added `xsl:include` with a relative `href` URL value resolve correctly you need to use `XElement xslt = XElement.Load(xXslPath, LoadOptions.SetBaseUri)`. – Martin Honnen Jul 11 '15 at 08:52