Home > Back-end >  How to save split file using xsl:result-document in Azure, FTP and SFTP through c# xsltTransformer?
How to save split file using xsl:result-document in Azure, FTP and SFTP through c# xsltTransformer?

Time:11-30

I am trying to split output into multiple file by using xsl:result-document. But not sure how to store the split result in Azure, FTP and SFTP server.

Input XML:

<ArrayOfBatch>
 <Batch>
  <BatchName>BatchName-1</BatchName>
 </Batch>
 <Batch>
  <BatchName>BatchName-2</BatchName>
 </Batch>
 <Batch>
  <BatchName>BatchName-3</BatchName>
 </Batch>
</ArrayOfBatch>

XSLT Format:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="3.0" exclude-result-prefixes="fn xs">
 <xsl:output method="xml" indent="yes"/>
 <xsl:template match="/">
  <xsl:for-each select="/ArrayOfBatch/Batch">
   <xsl:result-document href="Batch-{position()}.xml" >
    <xsl:copy-of select="*"/>
   </xsl:result-document>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

c# code:

public static string SaxonProcessorXSLT(string xmlToExport, string xslStylesheet)
{
  using (StringReader xmlStream = new StringReader(xmlToExport))
  {
    using (StringReader xslStream = new StringReader(xslStylesheet))
    {
      Processor xsltProcessor = new Processor();
      DocumentBuilder documentBuilder = xsltProcessor.NewDocumentBuilder();
      documentBuilder.BaseUri = new Uri("file://");
      XdmNode xdmNode = documentBuilder.Build(xmlStream);

      XsltCompiler xsltCompiler = xsltProcessor.NewXsltCompiler();
      XsltExecutable xsltExecutable = xsltCompiler.Compile(xslStream);
      XsltTransformer xsltTransformer = xsltExecutable.Load();
      xsltTransformer.InitialContextNode = xdmNode;
      xsltTransformer.BaseOutputUri = new Uri("D://home//data//");

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

From the above code the result of split file is stored in local path "D://home//data8//" but I am looking for a solution to store this result in azure, FTP and SFTP server by using URI on xsltTransformer.BaseOutputUri.

I have done some R&D on this but its not solving my problem. Looking for quick response. Thanks in advance.

CodePudding user response:

For the result documents, you can set a ResultDocumentHandler on the XsltTransformer (https://www.saxonica.com/html/documentation10/dotnetdoc/Saxon/Api/XsltTransformer.html#ResultDocumentHandler) that could then create a Serializer over a Stream or Writer a .NET (S)FTP client gives you.

CodePudding user response:

A assume you can use Serializer.SetOutputStream to provide a stream representing the remote file:

serializer.SetOutputStream(remoteFileStream);

Use your favorite FTP/SFTP library to create the output stream.

  • Related