XHTML 1.1 验证错误:“参数实体‘xhtml-inlstyle.mod’”引用本身”

发布于 2024-12-21 06:46:13 字数 1801 浏览 0 评论 0原文

嘿,我尝试使用官方 dtd

MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(MessageBody));
ms.Position = 0;
XmlReaderSettings settingsReader = new XmlReaderSettings();
settingsReader.DtdProcessing = DtdProcessing.Parse;
settingsReader.ValidationType = ValidationType.DTD;
MyUrlResolver resolver = new MyUrlResolver();
settingsReader.XmlResolver = resolver;
 XmlReader reader = XmlReader.Create(ms, settingsReader);
 while(reader.Read()){}

和自定义 XmlUrlResolver 验证我的 html 页面:

class MyUrlResolver : System.Xml.XmlUrlResolver
{
    public MyUrlResolver()
    { }

    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        if (File.Exists(System.Web.Hosting.HostingEnvironment.MapPath("~/dtd/xhtml11.dtd")))
        {
            absoluteUri = new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~/dtd/xhtml11.dtd"));
        }
        return base.GetEntity(absoluteUri, role, ofObjectToReturn);
    }

    public override Uri ResolveUri(Uri baseUri, string relativeUri)
    {
        baseUri = new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~"));
        relativeUri = "dtd/xhtml11.dtd";
        return base.ResolveUri(baseUri, relativeUri);
    }
}

在读取 xml 时发生验证,但出现异常:

参数实体“xhtml-inlstyle.mod”引用自身。 111 行,位置 21。

此处发生错误:

   <!-- Inline Style Module  ........................................ -->
<!ENTITY % xhtml-inlstyle.module "INCLUDE" >
<![%xhtml-inlstyle.module;[
<!ENTITY % xhtml-inlstyle.mod
     PUBLIC "-//W3C//ELEMENTS XHTML Inline Style 1.0//EN"
            "http://www.w3.org/MarkUp/DTD/xhtml-inlstyle-1.mod" >
%xhtml-inlstyle.mod;]]>

我无法理解为什么官方 dtd 是错误的:/我应该做什么?

Hey I try to validate my html page with official dtd :

MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(MessageBody));
ms.Position = 0;
XmlReaderSettings settingsReader = new XmlReaderSettings();
settingsReader.DtdProcessing = DtdProcessing.Parse;
settingsReader.ValidationType = ValidationType.DTD;
MyUrlResolver resolver = new MyUrlResolver();
settingsReader.XmlResolver = resolver;
 XmlReader reader = XmlReader.Create(ms, settingsReader);
 while(reader.Read()){}

and custom XmlUrlResolver:

class MyUrlResolver : System.Xml.XmlUrlResolver
{
    public MyUrlResolver()
    { }

    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        if (File.Exists(System.Web.Hosting.HostingEnvironment.MapPath("~/dtd/xhtml11.dtd")))
        {
            absoluteUri = new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~/dtd/xhtml11.dtd"));
        }
        return base.GetEntity(absoluteUri, role, ofObjectToReturn);
    }

    public override Uri ResolveUri(Uri baseUri, string relativeUri)
    {
        baseUri = new Uri(System.Web.Hosting.HostingEnvironment.MapPath("~"));
        relativeUri = "dtd/xhtml11.dtd";
        return base.ResolveUri(baseUri, relativeUri);
    }
}

Durning reading xml occurs validation and I get exception:

Parameter entity 'xhtml-inlstyle.mod' references itself. Line 111, position 21.

Error occurs here :

   <!-- Inline Style Module  ........................................ -->
<!ENTITY % xhtml-inlstyle.module "INCLUDE" >
<![%xhtml-inlstyle.module;[
<!ENTITY % xhtml-inlstyle.mod
     PUBLIC "-//W3C//ELEMENTS XHTML Inline Style 1.0//EN"
            "http://www.w3.org/MarkUp/DTD/xhtml-inlstyle-1.mod" >
%xhtml-inlstyle.mod;]]>

I cannot understand why official dtd is wrong :/ What should I do ?

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

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

发布评论

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

评论(3

长梦不多时 2024-12-28 06:46:14

您可以针对 xhtml11-flat.dtd 进行验证,它是以下内容的聚合xhtml11.dtd 以及所有xhtml11.dtd 引用的 *.mod 文件。

通过这种方式,您应该稍微调整自定义“MyUrlResolver”类以返回“xhtml11-flat.dtd”而不是“xhtml11.dtd”。

You could validate against xhtml11-flat.dtd, which is an aggregation of xhtml11.dtd and all the *.mod files referenced by xhtml11.dtd.

Doing it this way, you should adjust your custom 'MyUrlResolver' class a little bit to return the 'xhtml11-flat.dtd' instead of 'xhtml11.dtd'.

生活了然无味 2024-12-28 06:46:14

我的解决方案是下载 xhtml11.dtd 和引用 dtd 的所有 *.mod 文件。然后我在 dtd 中删除 http 链接到

    <!ENTITY % xhtml-datatypes.module "INCLUDE" >
<![%xhtml-datatypes.module;[
<!ENTITY % xhtml-datatypes.mod
     PUBLIC "-//W3C//ENTITIES XHTML Datatypes 1.0//EN"
            "xhtml-datatypes-1.mod" >
%xhtml-datatypes.mod;]]>

现在 dtd 在验证期间 html 页面可以使用本地 dtd 而无需从 www 下载;)

Solution for me was download xhtml11.dtd and all *.mod files referenced to dtd. Then I in dtd remove http links to

    <!ENTITY % xhtml-datatypes.module "INCLUDE" >
<![%xhtml-datatypes.module;[
<!ENTITY % xhtml-datatypes.mod
     PUBLIC "-//W3C//ENTITIES XHTML Datatypes 1.0//EN"
            "xhtml-datatypes-1.mod" >
%xhtml-datatypes.mod;]]>

Now dtd during validation html page can use local dtd without download it from www ;)

梦亿 2024-12-28 06:46:13

啊! DTD 有时会变得相当复杂。

让我们反汇编您的 DTD 片段。为了清楚起见,我重新包装了行并添加了行号。

1.    <!ENTITY % xhtml-inlstyle.module "INCLUDE" >
2.    <![%xhtml-inlstyle.module;[
3.    <!ENTITY % xhtml-inlstyle.mod PUBLIC "-//W3C//ELEMENTS XHTML Inline Style 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-inlstyle-1.mod" >
4.    %xhtml-inlstyle.mod;]]>

在这种形式中,我们可以说第 1 行有第 3 行是实体声明,第 2 行和第 4 行有包含实体引用的文本。

第一行是一个普通的旧文字值实体,我将添加替换文本来代替第 2 行的引用。为了增加清晰度,我将省略第一行,添加一些空格作为缩进和换行符。然后我们有:

2.    <![INCLUDE[
3.        <!ENTITY % xhtml-inlstyle.mod PUBLIC "-//W3C//ELEMENTS XHTML Inline Style 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-inlstyle-1.mod" >
4.        %xhtml-inlstyle.mod;
5.    ]]>

第 2 行成为带有 INCLUDE 关键字的标记节声明。第 3 行部分的内容是实体声明,由于关键字 PUBLIC,实体文本不是文字,而是外部实体声明。这意味着替换文本不是下面引用的文本,而是引用文档的内容,其位置由正式公共标识符和系统标识符(URL)指定。如果您幸运的话,遵循该行末尾的 URL 不会出现超时错误,您将看到此外部 DTD 的内容实际上是两个参数实体声明。它们是: 。通过扩展第 4 行上的实体引用,从技术上讲,您的原始 DTD 片段会产生以下 DTD 片段:

<![INCLUDE[
    <!ENTITY % style.attrib "style CDATA #IMPLIED">
    <!ENTITY % Core.extra.attrib "%style.attrib;" >
]]>

对我来说,这看起来并不太错误,但当然,我们赞赏同行评审。因此,下一个问题是:为什么会出现错误,什么可能导致错误?

我想到了一些可能性:

您处理的语法是否正确并且与此处显示的语法相同?如果第二个实体声明末尾缺少 > 字符,则在引用同一实体之前它不会终止(第 4 行)。只有当声明写在一行上时,解析才有效吗?尝试重新包装一下。解析器是否理解除具有文字值的实体声明之外的任何其他实体声明?尝试使用公共标识符创建类似的实体声明,但先不要引用它。该问题是否是由您的(公共/系统)标识符的解析方式引起的?您是否有 DTD 目录?是否通过网络将可能的 DTD 查找重定向到本地副本?如果将 URL 更改为本地文件(或不太可能超时的位置)会发生什么情况等。INCLUDE 实体声明周围的声明导致其失败?尝试将其移至 INCLUDE 声明上方,这样它也位于实体引用之前。 INCLUDE 是否有效,尝试使用我的最后一个 DTD 片段也会导致错误。

顺便一提。 XHTML 1.1 DTD 包含许多与此类似的其他包含结构,因此这可能不是唯一会引发错误的地方。这只是第一个。

我将以坏消息结束这篇文章。如果这个问题不是关于一些人为错误,例如真实 DTD 文件中的字符错放或丢失,或者这不是关于如何检索外部​​ DTD 资源的方式,那么我猜测这个问题是由错误/不支持的功能引起的在你的解析器上(可能不会很快得到修复)或者然后(尽管错误明确指向 DTD 文件)这是由你的 C# 代码中的某些内容引起的,与这里的其他人相比,我对此一无所知,并且可以'对你没有任何帮助 更远。无论如何,调试愉快!

Ugh! DTDs can get quite complex every now and then.

Let's disassemble your DTD snippet. For clarity I've re-wrapped the lines and added line numbers.

1.    <!ENTITY % xhtml-inlstyle.module "INCLUDE" >
2.    <![%xhtml-inlstyle.module;[
3.    <!ENTITY % xhtml-inlstyle.mod PUBLIC "-//W3C//ELEMENTS XHTML Inline Style 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-inlstyle-1.mod" >
4.    %xhtml-inlstyle.mod;]]>

In this form we could say that lines 1 have 3 are entity declarations, lines 2 and 4 have text containing an entity reference.

First line is a plain old literal value entity and I'll add the replacement text in place of the reference on line 2. To add clarity, I'll omit the first line, add some whitespace as indentation and a line feed. Then we have:

2.    <![INCLUDE[
3.        <!ENTITY % xhtml-inlstyle.mod PUBLIC "-//W3C//ELEMENTS XHTML Inline Style 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-inlstyle-1.mod" >
4.        %xhtml-inlstyle.mod;
5.    ]]>

Line 2 becomes a markup section declaration with INCLUDE keyword. The contents of the section in line 3 is an entity declaration for which the entity text is not a literal but an external entity declaration because of the keyword PUBLIC. This means that the replacement text is not the following quoted text, but the contents of the referenced document, which location is specified by that formal public identifier and system identifier (the URL). If you are lucky and following the URL at the end of the line won't give you a time out error, you'll see that the contents of this external DTD is practically two parameter entity declarations. They are: <!ENTITY % style.attrib "style CDATA #IMPLIED"> and <!ENTITY % Core.extra.attrib "%style.attrib;" >. By expanding the entity reference on line 4 your original piece of DTD technically results in this DTD snippet:

<![INCLUDE[
    <!ENTITY % style.attrib "style CDATA #IMPLIED">
    <!ENTITY % Core.extra.attrib "%style.attrib;" >
]]>

That doesn't look too erroneous to me, but peer review is appreciated of course. Therefore the next question is: why an error is raised, what could cause it?

Some possibilities that came to my mind:

Is the syntax you process correct and same as the one shown here? If the > character is missing from the end of the second entity declaration, it is not terminated before the same entity is referenced (on line 4). Does parsing the declaration work only if it is written on a single line? Try re-wrapping it. Does the parser understand any other entity declarations than the ones with literal value? Try creating a similar entity declaration with public identifier but don't refer to it first. Could the problem be caused by the way how your (public/system) identifiers are resolved? Do you have a DTD catalogue, are you redirecting the possible DTD lookups over the net to your local copies, what happens if you change the URL to a local file (or a place that is not likely time out) etc. Is the INCLUDE declaration around the entity declaration causing it to fail? Try moving it above the INCLUDE declaration so it is also way ahead of the entity reference. Does the INCLUDE work at all, try would using my last DTD snippet also cause an error.

By the way. XHTML 1.1 DTD contains many other include structures similar to this one, so this is probably not the only place which will raise an error. It was just the first one.

I'm gonna end this post to bad news. If this problem is not about some human error like misplaced or missing characters in your real DTD file, or if this is not about the way how the external DTD resources are retrieved, then I would guess that this problem is caused by bug/unsupported feature on your parser (which probably won't get fixed soon) or then (despite the error clearly pointing to the DTD file) this is caused by something in your C# code and compared to other folks here I have no clue about it and can't help you any further. Happy debugging anyway!

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