0

I am using saxon 9.8. I want to write a transform function in C# as below.So I have

 `using Saxon.Api;
  private string Transform(Stream xmlStream, string transform)
    {

    }`

Please help me fill this function

swifty
  • 165
  • 1
  • 1
  • 17
  • What is that function supposed to do exactly? If you want to transform the input `xmlStream` with the XSLT in the string `transform`, why would the result be an `XmlReader`? An XmlReader could serve as an input to a transformation. – Martin Honnen Nov 13 '17 at 07:57
  • 1
    Make sure you download the saxon-resources file and look at the sample applications making use of this API. – Michael Kay Nov 13 '17 at 09:22
  • @Martin Honen ,return a XmlReader is something I want for my application. That's ok. Return a string works but I se setting a baseUri in their manual transformer.SetInputStream( Stream input ,Uri baseUri); I have no idea what is this for. What Uri do I set? – swifty Nov 14 '17 at 09:12
  • See the documentation https://www.saxonica.com/html/documentation/dotnetdoc/Saxon/Api/XsltTransformer.html#SetInputStream(System.IO.Stream,System.Uri), it explains "baseUri: The base URI of the principal input document. This is used for example by the document() function if the document contains links to other documents in the form of relative URIs" – Martin Honnen Nov 14 '17 at 09:28

1 Answers1

1

I think you should at least try to write your code on your own and when you are stuck then look for possible answers on the internet. And only when you couldn't solve our problem on this way than asked a specific question. I am not going to write your whole code for you. But i can share here how i am using the Saxon API for XSLT transformation:

    public static String TransfromXMLWithXSLT2(String i_xmlData, String i_xsltCode)
    {
        Processor xsltProcessor = new Processor();
        DocumentBuilder documentBuilder = xsltProcessor.NewDocumentBuilder();
        documentBuilder.BaseUri = new Uri("file://");
        XdmNode xdmNode = documentBuilder.Build(new StringReader(i_xmlData));

        XsltCompiler xsltCompiler = xsltProcessor.NewXsltCompiler();
        XsltExecutable xsltExecutable = xsltCompiler.Compile(new StringReader(i_xsltCode));
        XsltTransformer xsltTransformer = xsltExecutable.Load();
        xsltTransformer.InitialContextNode = xdmNode;

        using (StringWriter stringWriter = new StringWriter()) {
            Serializer serializer = new Serializer();
            serializer.SetOutputWriter(stringWriter);
            xsltTransformer.Run(serializer);
            return stringWriter.ToString();
        }
    }

I am using a switch in my transformation class to support XSLT1.0 too. For XSLT1.0 I prefer the .Net functionality, just for your info it looks like this in my code:

    public static String TransfromXMLWithXSLT1(String i_xmlData, String i_xsltCode)
    {
        XslCompiledTransform xslt = new XslCompiledTransform();

        using (StringReader xsltStringReader = new StringReader(i_xsltCode)) {
            using (XmlReader xsltXmlReader = XmlReader.Create(xsltStringReader)) {
                xslt.Load(xsltXmlReader);
                using (StringReader tableStringReader = new StringReader(i_xmlData)) {
                    using (XmlReader tableXmlReader = XmlReader.Create(tableStringReader)) {
                        using (StringWriter stringWriter = new StringWriter()) {
                            xslt.Transform(tableXmlReader, new XsltArgumentList(), stringWriter);
                            return stringWriter.ToString();
                        }
                    }
                }
            }
        }
    }

So if you need Streams/Readers and not Strings simply change the code in the way it is needed!

Momo
  • 456
  • 5
  • 22
  • What is documentBuilder.BaseUri = new Uri("file:///C:/"); for? Is it mandatory? What if I dont need that? – swifty Nov 14 '17 at 09:07
  • Good question! It is mandatory but i don't know why exactly. I guess it is because Saxon is especially made for handle with files. Normally DocumentBuilder creates a new document on your system. Put i'm only useing it to create a XdmNode for the XsltTransformer. But it seems to be enough to right new Uri("file://"). Saw this in other blogs to => https://stackoverflow.com/questions/16101213/load-xml-and-xslt-from-embedded-resource-in-saxon-9-4he – Momo Nov 14 '17 at 14:13
  • 1
    There's some discussion on this question at https://saxonica.plan.io/boards/3/topics/7029 – Michael Kay Nov 15 '17 at 12:05
  • After adding from nuget (with : Install-Package Saxon-HE -Version 9.8.0.15) , function helped me – kordiseps Jan 17 '19 at 14:25