Steps to perform transformation from SQL to XML to XSLT Output data (csv, htm, tab-delim, fixed-width, etc.):
1). Get your SQL data (assuming you are using SQL Server) into XML format via:
2). Load XSL transform file (OrderXform.xsl) into a new XslCompiledTransform object (transform):
3). Using a StringWriter (writer), StringReader (inputReader) and XmlReader (inputXmlReader), read the inputReader XML string into inputXmlReader and apply the XSL transformation to the transform object via: transform.Transform(inputXmlReader, args, writer); //(or another override of Transform())
4). Return the result and write the bits (to memory as Label Text for a demo like this, to file, to domain network location, to non-domain customer ftp, etc.).
Nothing incredibly complex about this, but it seems to frustrate a lot of people in .NET land so I have documented a quick and easy example.
GitHub Source: https://github.com/Radagast27/Simpl_XSLT_dot_net
Tool for XSLT validation: https://www.freeformatter.com/xsl-transformer.html
References:
https://www.red-gate.com/simple-talk/sql/learn-sql-server/using-the-for-xml-clause-to-return-query-results-as-xml/
https://www.w3schools.com/xml/xsl_examples.asp
https://www.c-sharpcorner.com/article/transform-xml-output-of-sql-query-using-for-xml-auto-statement-to-html-using-xsl/
1). Get your SQL data (assuming you are using SQL Server) into XML format via:
SELECT [OrderId], [PersonId], [OrderDateTime] FROM [Order] FOR XML AUTO, ELEMENTS, ROOT ('Orders');
XML Result of "FOR XML AUTO, ELEMENTS, ROOT"..
2). Load XSL transform file (OrderXform.xsl) into a new XslCompiledTransform object (transform):
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("C:\\Xform\\OrderXform.xsl");
XSL file used to transform the XML to the desired file output (EDI 834 standard, etc)
3). Using a StringWriter (writer), StringReader (inputReader) and XmlReader (inputXmlReader), read the inputReader XML string into inputXmlReader and apply the XSL transformation to the transform object via: transform.Transform(inputXmlReader, args, writer); //(or another override of Transform())
using (StringWriter writer = new StringWriter())
{
// Make an XML reader (inputReader) out of the string (input)
XmlReader inputXmlReader;
using (var inputReader = new StringReader(input))
{
inputXmlReader = XmlReader.Create(inputReader);
// Apply the transformation to XmlReader (inputXmlReader) and write it to StringWriter (writer)
transform.Transform(inputXmlReader, null, writer);
}
// Retrieve the output as string from writer object
return writer.GetStringBuilder().ToString();
}
4). Return the result and write the bits (to memory as Label Text for a demo like this, to file, to domain network location, to non-domain customer ftp, etc.).
The result normally gets written to remote SFTP location or posted to API endpoint for processing
Nothing incredibly complex about this, but it seems to frustrate a lot of people in .NET land so I have documented a quick and easy example.
GitHub Source: https://github.com/Radagast27/Simpl_XSLT_dot_net
Tool for XSLT validation: https://www.freeformatter.com/xsl-transformer.html
References:
https://www.red-gate.com/simple-talk/sql/learn-sql-server/using-the-for-xml-clause-to-return-query-results-as-xml/
https://www.w3schools.com/xml/xsl_examples.asp
https://www.c-sharpcorner.com/article/transform-xml-output-of-sql-query-using-for-xml-auto-statement-to-html-using-xsl/