将多个 xml 文件传递​​到 XSLT 转换 C#

Pass multiple xml files into XSLT transform C#

我正在尝试将以下 xslt 转换为引用内存中 'audio' xml 文件而不是物理 audio.xml 文件。 以下 xslt 文件适用于物理 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:custom="custom-functions">
    <xsl:output method="xml" indent="yes" version="1.0" encoding="ISO-8859-1"/>

    <xsl:param name="audioxml" select="'./audio.xml'"/>

    <xsl:variable name="audiofile" select="document($audioxml)"/>

    <xsl:template match="/">
        <xsl:for-each select="bookstore" >
            <xsl:for-each select="book" >
                <booktitle>
                    <xsl:value-of select="title" />
                </booktitle>
            </xsl:for-each>
        </xsl:for-each>

        <xsl:for-each select="$audiofile">
            <xsl:for-each select="audiostore" >
                <xsl:for-each select="audio" >
                    <audiotitle>
                        <xsl:value-of select="title" />
                    </audiotitle>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

book.xml

<?xml version=\"1.0\" encoding=\"utf-8\" ?><bookstore><book genre=\"autobiography\" publicationdate=\"1981\" ISBN=\"1-861003-11-0\"><title>The Autobiography of Benjamin Franklin</title><author><first-name>Benjamin</first-name><last-name>Franklin</last-name></author><price>8.99</price></book></bookstore>

audio.xml

<?xml version=\"1.0\" encoding=\"utf-8\" ?><audiostore><audio genre=\"autobiography\" publicationdate=\"1981\" ISBN=\"1-861003-11-0\"><title>The Autobiography of Benjamin Franklin 2</title><author><first-name>Benjamin 2</first-name><last-name>Franklin 2</last-name></author><price>8.99</price></audio></audiostore>

所以我试图在内存中传递 xml 文件,但以下代码抱怨 An error occurred while loading document '/file2.xml'

public static string MergeXml(string xml1, string xml2) {
    XslCompiledTransform xslt = new XslCompiledTransform();
    XmlDocument xsltDoc = new XmlDocument();
    // Load the XSLT file through XmlReader to override the base URI.
    using (StreamReader reader = File.OpenText(@"template.xsl"))
    using (XmlReader xmlReader = XmlReader.Create(reader, null, "file:///template.xsl"))
    {
        xsltDoc.Load(xmlReader);
    }
    // Use XsltSettings to enable the use of the document() function.
    xslt.Load(xsltDoc, new XsltSettings(true, false), null);

    // Load the first XML file into a document
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml1);

    // Create the resolver and add the second file to it.
    XmlPreloadedResolver resolver = new XmlPreloadedResolver();
    resolver.Add(new Uri("file:///file2.xml"), xml2);

    using (StringWriter writer = new StringWriter())
    using (XmlWriter xmlWriter = XmlWriter.Create(writer))
    {
        // Pass the resolver to the transform
        xslt.Transform(doc, null, xmlWriter, resolver);
        return writer.ToString();
    }
}

似乎,XmlPreloadedResolver 的 Add 方法在获取字符串时将其解析为 UTF-16/Encoding.NET 中的 Unicode,因此要么不要放置任何 XML 声明在传递给 Add 方法的字符串上,或确保它们声明 UTF-16,例如var xml2 = "<?xml version=\"1.0\" encoding=\"utf-16\" ?><audiostore>...