Ajeet Singh
Ajeet Singh

Reputation: 3

Pass xml and xsl as string for processing in xslt 3

I am converting xml to html using xslt 3.0 saxon-HE 9.8 library. Using it in c# code.

I am passing xml and xslt file path in input to get it transformed and get output.

Can anyone please let me know how can I pass xml as string and xslt as string as input in c# code for processing it.

Below is my code.

public static string Transform_XML(string param, string inputfile, string xsltfilename)
    {
        var xslt = new FileInfo(xsltfilename);
        var input = new FileInfo(inputfile);

        // Compile stylesheet
        var processor = new Processor();
        var compiler = processor.NewXsltCompiler();
        var executable = compiler.Compile(new Uri(xslt.FullName));

        XPathDocument doc = new XPathDocument(new StringReader(param));
        DocumentBuilder db = processor.NewDocumentBuilder();
        XdmNode xml;
        using (XmlReader xr = XmlReader.Create(new StringReader(param)))
        {
            xml = db.Build(xr);
        }

        // Do transformation to a destination
        var destination = new DomDestination();
        using (var inputStream = input.OpenRead())
        {
            var transformer = executable.Load();
            transformer.SetParameter(new QName("", "", "user_entry"), xml);
            transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
            transformer.Run(destination);
        }

        return destination.XmlDocument.InnerXml.ToString();
    }

Want to pass xml and xslt as string instead of file path.

UPDATE 1

Got the solution for passing xml and xsl as string in c#. Below is the updated code.

private string Transform_XML(string param, string param_name, string inputfile, string xsltfilename)
    {
        string xslt_input = System.IO.File.ReadAllText(xsltfilename + ".xslt");
        string xml_input = System.IO.File.ReadAllText(inputfile + ".xml");

        // Compile stylesheet
        var processor = new Processor();
        var compiler = processor.NewXsltCompiler();
        compiler.BaseUri=new Uri(Server.MapPath("/"));
        var executable = compiler.Compile(new XmlTextReader(new StringReader(xslt_input)));

        XPathDocument doc = new XPathDocument(new StringReader(param));
        DocumentBuilder db = processor.NewDocumentBuilder();
        XdmNode xml;
        using (XmlReader xr = XmlReader.Create(new StringReader(param)))
        {
            xml = db.Build(xr);
        }

        //xml input
        DocumentBuilder builder = processor.NewDocumentBuilder();
        builder.BaseUri= new Uri(Server.MapPath("/"));
        MemoryStream ms = new MemoryStream();
        StreamWriter tw = new StreamWriter(ms);
        tw.Write(xml_input);
        tw.Flush();
        Stream instr = new MemoryStream(ms.GetBuffer(), 0, (int)ms.Length);
        XdmNode input = builder.Build(instr);

        // Do transformation to a destination
        var destination = new DomDestination();
        var transformer = executable.Load();

        //Set the parameter with xml value
        transformer.SetParameter(new QName("", "", param_name), xml);

        // Set the root node of the source document to be the initial context node
        transformer.InitialContextNode = input;
        transformer.Run(destination);

        // Get result 
        return destination.XmlDocument.InnerXml.ToString();
    }

Upvotes: 0

Views: 755

Answers (1)

Michael Kay
Michael Kay

Reputation: 163272

The XsltTransformer has a method SetInputStream() that allows you to supply the input as a stream (which indeed you appear to be using).

This post How do I generate a stream from a string? tells you how to create a stream from a string.

Upvotes: 1

Related Questions