将 xml 和 xsl 作为字符串传递给 xslt 3 进行处理

Pass xml and xsl as string for processing in xslt 3

我正在使用 xslt 3.0 saxon-HE 9.8 库将 xml 转换为 html。在 C# 代码中使用它。

我在输入中传递 xml 和 xslt 文件路径以对其进行转换并获得输出。

任何人都可以告诉我如何将 xml 作为字符串和 xslt 作为字符串作为输入传递给 c# 代码以进行处理。

下面是我的代码。

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();
    }

想要将 xml 和 xslt 作为字符串而不是文件路径传递。

更新 1

获得了在 c# 中将 xml 和 xsl 作为字符串传递的解决方案。下面是更新后的代码。

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();
    }

XsltTransformer 有一个方法 SetInputStream() 允许您将输入作为流提供(实际上您似乎正在使用)。

此 post How do I generate a stream from a string? 告诉您如何从字符串创建流。