XmlResolver:XSLT 编译器错误

发布于 2024-11-16 07:30:48 字数 2321 浏览 2 评论 0原文

我在使用 XmlResolver 类时遇到问题。我在 MS SQL 数据库中的 xml 数据类型列中保存了一些 XSLT 文件。我正在尝试编写一个 XmlResolver 类实现,它将从数据库而不是从文件加载文本。但我收到 XSLT 编译器错误。 这是非常简单的示例(输入和 xslt 的文本均在此处进行硬编码):

    static void Main(string[] args)
    {
        string xslt = @"<?xml version=""1.0"" encoding=""utf-8""?>
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""     xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" exclude-result-prefixes=""msxsl"" >
<xsl:import href=""test.xslt"" />
<xsl:output method=""xml"" indent=""yes""/>
<xsl:template match=""*"">
    <xsl:value-of select=""$MyVariable""/>
</xsl:template>
</xsl:stylesheet>";
        XDocument transformationInput = XDocument.Parse("<test />");
        myResolv res = new myResolv();
        XslCompiledTransform transform = new XslCompiledTransform(true);
        XsltSettings sett = new XsltSettings(true, true);
        StringReader transr = new StringReader(xslt);
        XmlReader tranReader = XmlReader.Create(transr); 
        transform.Load(tranReader, sett, res);
    }
}

这是非常简单的 XmlResolver 类:

class myResolv : XmlResolver
{
    public override Uri ResolveUri(Uri baseUri, string relativeUri)
    {
        return base.ResolveUri(baseUri, relativeUri);
    }

    public override System.Net.ICredentials Credentials
    {
        set { throw new NotImplementedException(); }
    }

    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        string fileName = System.IO.Path.GetFileName(absoluteUri.ToString());
        if (fileName == "test.xslt")
        {
            string newXslt = @"<?xml version=""1.0"" encoding=""utf-8""?>
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""     xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" exclude-result-prefixes=""msxsl"" >
<xsl:variable name=""MyVariable"" select=""1"" />
  </xsl:stylesheet>";
            StringReader read = new StringReader(newXslt);
            XmlReader xmlread = XmlReader.Create(read);
            return xmlread;
        }
        else
            throw new NotImplementedException();
    }
}

Transform.Load 行执行失败(XSLT 编译器错误)。从文件读取转换时,解析器工作正常。但我不想从文件中读取它。 谢谢, 彼得

I have troubles with XmlResolver class. I have a few XSLT files saved in MS SQL database in xml datatype column. I'm trying to write a XmlResolver class implementation, that would load the text from database instead of from the files. But I'm getting XSLT compiler error.
Here is very simple example (text of both input and xslt is hardcoded here):

    static void Main(string[] args)
    {
        string xslt = @"<?xml version=""1.0"" encoding=""utf-8""?>
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""     xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" exclude-result-prefixes=""msxsl"" >
<xsl:import href=""test.xslt"" />
<xsl:output method=""xml"" indent=""yes""/>
<xsl:template match=""*"">
    <xsl:value-of select=""$MyVariable""/>
</xsl:template>
</xsl:stylesheet>";
        XDocument transformationInput = XDocument.Parse("<test />");
        myResolv res = new myResolv();
        XslCompiledTransform transform = new XslCompiledTransform(true);
        XsltSettings sett = new XsltSettings(true, true);
        StringReader transr = new StringReader(xslt);
        XmlReader tranReader = XmlReader.Create(transr); 
        transform.Load(tranReader, sett, res);
    }
}

And here is very simple XmlResolver class:

class myResolv : XmlResolver
{
    public override Uri ResolveUri(Uri baseUri, string relativeUri)
    {
        return base.ResolveUri(baseUri, relativeUri);
    }

    public override System.Net.ICredentials Credentials
    {
        set { throw new NotImplementedException(); }
    }

    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        string fileName = System.IO.Path.GetFileName(absoluteUri.ToString());
        if (fileName == "test.xslt")
        {
            string newXslt = @"<?xml version=""1.0"" encoding=""utf-8""?>
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""     xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" exclude-result-prefixes=""msxsl"" >
<xsl:variable name=""MyVariable"" select=""1"" />
  </xsl:stylesheet>";
            StringReader read = new StringReader(newXslt);
            XmlReader xmlread = XmlReader.Create(read);
            return xmlread;
        }
        else
            throw new NotImplementedException();
    }
}

The execution fails on Transform.Load row (XSLT Compiler Error). When reading the transformation from a file, resolver works fine. But I do not want to read it from a file.
Thanks,
Petr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2024-11-23 07:30:48

问题在于它用于关联每个文件的 base-uri(通过 XmlReader.BaseUri)。幸运的是,修复方法很简单;在 GetEntity 中:

XmlReader xmlread = XmlReader.Create(read, null, fileName);

请注意,这意味着实体的逻辑名称(用于相对解析)现在是 test.xslt。在您的情况下,这很好,但如果路径使用文件夹结构,您需要小心确保它们是相对/根正确的。

The problem is the base-uri that it uses to associate each file (via XmlReader.BaseUri). The fix is fortunately simple; in GetEntity:

XmlReader xmlread = XmlReader.Create(read, null, fileName);

Note that this means the logical name of the entity (for relative resolution) is now test.xslt. In your case that is fine, but if the path was using a folder structure you would need to be careful to ensure they are relative/rooted correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文