Project

Profile

Help

Support #4087 » Program.cs

Martin Honnen, 2019-01-08 12:04

 
using Saxon.Api;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ResultDocDocTypeMissing
{
class Program
{
static void Main(string[] args)
{
Processor proc = new Processor(false);

string xslt = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='3.0' expand-text='yes'><xsl:output method='html' indent='yes' html-version='5.0'/><xsl:template match='/' name='xsl:initial-template'><html><head><title>test</title></head><body><h1>Test</h1></body></html><xsl:for-each select='1 to 3'><xsl:result-document href='result-{.}.html'><html><head><title>Result test {.}</title></head><body><h1>Result {.}</h1></body></html></xsl:result-document></xsl:for-each></xsl:template></xsl:stylesheet>";

using (StringReader sr = new StringReader(xslt))
{
XsltTransformer transformer = proc.NewXsltCompiler().Compile(sr).Load();

transformer.InitialTemplate = new QName("http://www.w3.org/1999/XSL/Transform", "initial-template");

transformer.Run(proc.NewSerializer(Console.Out));

Console.WriteLine();

MyResultDocumentHandler resDocHandler = new MyResultDocumentHandler(proc);

transformer.ResultDocumentHandler = resDocHandler;

transformer.Run(proc.NewSerializer(Console.Out));

foreach (MySerializer ser in resDocHandler.Serializers)
{
Console.WriteLine("{0}:\n{1}", ser.Href, ser.Result);
Console.WriteLine();
}
}
}
}

internal class MyResultDocumentHandler : IResultDocumentHandler
{
private Processor proc;

public List<MySerializer> Serializers { get; set; }

public MyResultDocumentHandler(Processor proc)
{
this.proc = proc;
Serializers = new List<MySerializer>();
}

public XmlDestination HandleResultDocument(string href, Uri baseUri)
{
var ser = new MySerializer(proc, href);
Serializers.Add(ser);
return ser;
}
}

internal class MySerializer : Serializer
{
private StringWriter resultWriter;

public string Result { get; private set; }

public string Href { get; }

public MySerializer(Processor processor, string href)
{
SetProcessor(processor);

Href = href;

resultWriter = new StringWriter();

SetOutputWriter(resultWriter);

}

public override void Close()
{
Result = resultWriter.ToString();
resultWriter.Close();
resultWriter.Dispose();
base.Close();
}
}
}
    (1-1/1)