【问题标题】:Pass multiple xml files into XSLT transform C#将多个 xml 文件传递​​到 XSLT 转换 C#
【发布时间】:2022-01-02 14:48:59
【问题描述】:

我正在尝试将以下 xslt 转换为引用内存中的“音频”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 resolver = new XmlPreloadedResolver(); resolver.Add(new Uri("file:///file2.xml"), xml2); 设置了解析器,来自XSLT 的document('file:///file2.xml') 是否工作?
  • 你有堆栈跟踪吗?尝试从传递给Add 方法的字符串中删除&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;
  • @MartinHonnen 它抛出的错误消息是 XmlException: Name cannot begin with the '.' character, 并更改了 Uri 它抱怨“无法解析 file2.xml”。无论如何,正如你所建议的那样,我将&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt; 丢弃在audio..xml 中,它成功了。非常感谢。

标签: c# xml xslt xslcompiledtransform


【解决方案1】:

看来,XmlPreloadedResolver 的 Add 方法在获取字符串时会将其解析为 .NET 中的 UTF-16/Encoding.Unicode,因此不要在传递给 @ 的字符串上放置任何 XML 声明987654322@ 方法或确保他们声明 UTF-16,例如var xml2 = "&lt;?xml version=\"1.0\" encoding=\"utf-16\" ?&gt;&lt;audiostore&gt;...

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多