Project

Profile

Help

Bug #2296 » Program.cs

Emanuel Wlaschitz, 2015-01-20 11:19

 
using System;
using System.IO;
using System.Xml.Linq;
using Saxon.Api;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XsltExecutable xsltExecutable;
var processor = new Processor(false);

//sample content for the XSLT here:
// <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
// <xsl:output omit-xml-declaration="yes"/>
// <xsl:template match="/"><output1/><output2/></xsl:template>
// </xsl:stylesheet>
string fileName = @"path\to\xslt\with\omit-xml-declaration.xsl";

Uri stylesheetUri;
if (!Uri.TryCreate(fileName, UriKind.RelativeOrAbsolute, out stylesheetUri))
throw new ArgumentException(string.Format("Could not create an URI from stylesheet path '{0}'.", fileName), "fileName");
using (var stylesheet = File.OpenRead(fileName))
{
var compiler = processor.NewXsltCompiler();
compiler.BaseUri = stylesheetUri; //required to resolve stylesheet includes
xsltExecutable = compiler.Compile(stylesheet);
}

var xDocument = new XDocument(new XElement("input"));
var transformer = xsltExecutable.Load();
var builder = processor.NewDocumentBuilder();
transformer.InitialContextNode = builder.Build(xDocument.CreateReader());

string result;
//using (var ms = new MemoryStream())
using (var stringWriter = new StringWriter())
{
var destination = new Serializer();
destination.SetOutputWriter(stringWriter);
//destination.SetOutputStream(ms);

//the actual result here is '<?xml version="1.0" encoding="UTF-8"?><output1/><output2/>'
//however, expected would be '<output1/><output2/>'
//the same happens when using a Stream, so it doesn't seem to be the TextWriter's fault...
transformer.Run(destination);
result = stringWriter.ToString();
//result = Encoding.UTF8.GetString(ms.ToArray());
}
Console.WriteLine(result);
}
}
}
    (1-1/1)