具有自定义结果文档处理程序的 Saxon 序列化程序选项

Saxon serializer options with custom result document handler

您好,我实现了一个自定义结果文档处理程序来解析相关 uris 并跟踪写入的文件。

private class ResultDocumentHandler : IResultDocumentHandler
{
    private List<string> writtenFiles = new List<string>();

    public List<string> WrittenFiles => this.writtenFiles;

    public XmlDestination HandleResultDocument(string href, Uri baseUri)
    {
        if (href.Contains("%"))
            href = Uri.UnescapeDataString(href);

        try
        {
            Uri hrefUri = new Uri(href, true);

            if (hrefUri.IsAbsoluteUri)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(hrefUri.LocalPath));

                Serializer serializer = new Serializer();

                serializer.SetOutputFile(hrefUri.LocalPath);
                writtenFiles.Add(hrefUri.LocalPath);

                return serializer;
            }
        }
        catch
        {
            // ignore
        }

        try
        {
            Uri absoluteUri = new Uri(baseUri, href, true);

            Directory.CreateDirectory(Path.GetDirectoryName(absoluteUri.LocalPath));

            Serializer serializer = new Serializer();

            serializer.SetOutputFile(absoluteUri.LocalPath);
            writtenFiles.Add(absoluteUri.LocalPath);

            return serializer;
        }
        catch
        {
            // ignore
        }

        return new NullDestination();
    }
}

到目前为止一切顺利。但是当我创建一个新的序列化程序时,它不会使用 xsl 文件中提供的选项。例如我用这个:

<xsl:result-document href="{$resultDoc}" method="html" omit-xml-declaration="yes" indent="no">

这些选项现在被忽略了,因为我没有为序列化程序设置它们。但是我怎样才能在结果文档处理程序中访问它们呢?我想我可以使用 Saxon.Api.XsltTransformer.GetOutputProperties() 访问 xsl:output 的选项,但我需要特定 xsl:result-document.

的选项

有什么办法吗?

我认为没有任何简单的方法可以做到这一点。我已经在

记录了这个

https://saxonica.plan.io/issues/3153

你可以做的是忽略Saxon.Api包中的ResultDocumentHandler机制,并下拉到Java定义的OutputURIResolver。您可以编写一个实现 OutputURIResolver 接口的 C# class 和一个 StreamResult returns,并将其设置在支撑 XsltTransformer 的控制器接口上。这很麻烦,因为您必须将 saxon9 DLL 和一些 IKVM DLL 添加到您的项目中,以便您可以针对基础 Java API 的 IKVM 提供的 C# 转换进行编程。我想这就是我们添加 ResultDocumentHandler 接口的原因,但我们对用例考虑得不够仔细。