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
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
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!