XmlResolver:XSLT 编译器错误 [英] XmlResolver: XSLT compiler error

查看:30
本文介绍了XmlResolver:XSLT 编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 XmlResolver 类时遇到问题.我在 xml 数据类型列中的 MS SQL 数据库中保存了一些 XSLT 文件.我正在尝试编写一个 XmlResolver 类实现,它将从数据库而不是从文件加载文本.但我收到 XSLT 编译器错误.这是一个非常简单的例子(输入和 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);
    }
}

这是一个非常简单的 XmlResolver 类:

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();
    }
}

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

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

推荐答案

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

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);

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

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.

这篇关于XmlResolver:XSLT 编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆