““http://www.w3.org/XML/1998/namespace:lang”属性未声明。”

发布于 2024-11-09 09:19:36 字数 1902 浏览 0 评论 0原文

有时,当使用 XmlValidatingReader 验证某些 XML 文档时,我会收到以下错误:

System.Xml.Schema.XmlSchemaValidationException: 
"The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared."

同一文档有时会成功。我不明白为什么。

我的 XSD 导入架构如下:

<xs:schema id="myschemaId"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="http://mytargetnamespace.com"
       xmlns="http://mytargetnamespace.com"
       xmlns:mm="http://mytargetnamespace.com"
       elementFormDefault="qualified">
 <xs:import namespace="http://www.w3.org/XML/1998/namespace" 
            schemaLocation="http://www.w3.org/2001/xml.xsd" />
 ...

在 XML 文档中,我具有以下属性:

<root xmlns="http://mytargetnamespace.com"        
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://mytargetnamespace.com myschema.xsd">

最后,XmlReaderSettings:

const XmlSchemaValidationFlags validationFlags =
          XmlSchemaValidationFlags.ProcessInlineSchema |
          XmlSchemaValidationFlags.ProcessSchemaLocation |  
          XmlSchemaValidationFlags.ReportValidationWarnings |
          XmlSchemaValidationFlags.AllowXmlAttributes;

// Set the validation settings.
var settings = new XmlReaderSettings
                   {
                       ValidationType = ValidationType.Schema,
                       ValidationFlags = validationFlags,
                       DtdProcessing = DtdProcessing.Parse
                   };
settings.ValidationEventHandler += OnValidationEventHandler;

// Create the XmlReader object.
var reader = XmlReader.Create(_xmlFilePath, settings);

// Parse the file. 
while (reader.Read()) {}

这是一个在 Windows 2003 上运行 .NET 4.0 的独立 exe。

我注意到,当它尝试执行操作时,会出现明显的停顿。证实。这可能有关系吗?它是否尝试下载实际的“xml.xsd”架构但没有成功?

Sometimes, when validating certain XML documents using an XmlValidatingReader, I receive the following error:

System.Xml.Schema.XmlSchemaValidationException: 
"The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared."

The same document sometimes succeeds. I cannot figure out why.

My XSD imports the schema like so:

<xs:schema id="myschemaId"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="http://mytargetnamespace.com"
       xmlns="http://mytargetnamespace.com"
       xmlns:mm="http://mytargetnamespace.com"
       elementFormDefault="qualified">
 <xs:import namespace="http://www.w3.org/XML/1998/namespace" 
            schemaLocation="http://www.w3.org/2001/xml.xsd" />
 ...

And in the XML document I have the following attributes:

<root xmlns="http://mytargetnamespace.com"        
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://mytargetnamespace.com myschema.xsd">

Finally, the XmlReaderSettings:

const XmlSchemaValidationFlags validationFlags =
          XmlSchemaValidationFlags.ProcessInlineSchema |
          XmlSchemaValidationFlags.ProcessSchemaLocation |  
          XmlSchemaValidationFlags.ReportValidationWarnings |
          XmlSchemaValidationFlags.AllowXmlAttributes;

// Set the validation settings.
var settings = new XmlReaderSettings
                   {
                       ValidationType = ValidationType.Schema,
                       ValidationFlags = validationFlags,
                       DtdProcessing = DtdProcessing.Parse
                   };
settings.ValidationEventHandler += OnValidationEventHandler;

// Create the XmlReader object.
var reader = XmlReader.Create(_xmlFilePath, settings);

// Parse the file. 
while (reader.Read()) {}

This is a standalone exe running .NET 4.0 on Windows 2003.

I've noticed that there's a significant pause when it's trying to validate. Could that be related? Is it trying to download the actual "xml.xsd" schema and not succeeding?

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

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

发布评论

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

评论(2

-小熊_ 2024-11-16 09:19:36

由于许多 DTD 和 XSD 源自 W3C,因此存在这样的问题:许多人尝试从其服务器解析它们,导致它们被请求淹没——数以百万计的请求。所以他们开始阻止“过多”的请求。

请参阅此博客文章,它也适用于 XSD。

解决方案是使用本地副本。

Because many of the DTDs and XSDs originated from the W3C, they have the problem that many people try to resolve them from their servers, resulting in their being inundated with requests - millions and millions of them. So they started blocking "excessive" requests.

See this blog entry, which also applies to XSDs.

The solution is to use a local copy.

々眼睛长脚气 2024-11-16 09:19:36

我非常有信心我已经解决了这个问题。我检查了 Fiddler,确实看到了向 w3c.org 发送 xsd 文件的请求。更多的研究出现了此链接;评论#3 似乎与我的情况有关。因此,如果出于某种原因我的计算机无法下载 XSD 文件,则 xml 命名空间将变得不可用。遗憾的是,真正的错误(“无法到达 w3c.org”或其他什么)从未被报告过。

xs:import 中删除 schemaLocation 就可以了。

I'm pretty confident I've solved this one. I checked Fiddler and did see requests going out to w3c.org for the xsd file. A little more research turned up this link; remark #3 seemed to relate to my situation. So if for whatever reason my machine couldn't download the XSD file, then the xml namespace became unavailable. Sadly the real error ("could not reach w3c.org" or what have you) was never reported.

Removing the schemaLocation from the xs:import did the trick.

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