通过 XmlResolver 处理 .Net 中的公共外部 DTD

发布于 2024-08-22 03:49:26 字数 722 浏览 8 评论 0原文

我的 XML 引用这样的 DTD:

< !DOCTYPE 文章 PUBLIC "-//OWNER//NAME//EN" "http://invalid/path/to .dtd">

DTD 无法通过给定的 URL 获得,但我可以将其下载到我的光盘上。我尝试实现自定义 XmlResolver 来加载 DTD,但它不起作用。我的自定义 XmlResolver 实现了 GetEntity,通过调试器我可以看到以下调用:

  1. 请求的 uri 是要加载的 xml 文档。我为此文档打开一个流并将其返回。效果很好。
  2. DTD 被请求为格式为“file:///absolut/path/to.xml/-//OWNER//NAME//EN”的 URI。我正在使用正则表达式来检查 -//.*?// ,它工作正常,但对我来说看起来不太干净。但如果 DTD 是独立的,它就可以工作。
  3. DTD 引用了modules.ent。这会导致调用 GetEntity,其 URI 为:“file:///absolut/path/to.xml/-//OWNER//NAME//modules.ent”。显然,现在重构路径的内涵变得很奇怪。

有什么提示如何以正确的方式实现这一点吗?我认为公共外部 DTD 在出版领域很常见,所以必须有一个干净的解决方案!?

干杯, 阿希姆

my XML is referencing a DTD like this:

< !DOCTYPE article PUBLIC "-//OWNER//NAME//EN" "http://invalid/path/to.dtd">

The DTD is not available via the given URL, but I can download it to my disc. I tried to implement a custom XmlResolver to load the DTD, but it does not work. My custom XmlResolver implements GetEntity and via the debugger I can see the following calls comming in:

  1. The requested uri is the xml document to be loaded. I open a stream for this document and return it. That works fine.
  2. The DTD is requested as a URI of the format "file:///absolut/path/to.xml/-//OWNER//NAME//EN". I'm using a regular expression to check for -//.*?// which works fine, but looks not very clean for me. But if the DTD is selfcontained, it works.
  3. The DTD is referencing modules.ent. That results in a call to GetEntity with an URI of: "file:///absolut/path/to.xml/-//OWNER//NAME//modules.ent". Obviously now it get's quite strange to reconstruct the intension of the path.

Any hint how to implement that in a correct way? I think public external DTDs are quite common in the publishing sector so there must be a clean solution!?

cheers,
Achim

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

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

发布评论

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

评论(1

甜扑 2024-08-29 03:49:26

"file:///absolut/path/to.xml/-//OWNER//NAME//EN" 是 SYSTEM 和 PUBLIC 标识符的串联。一般来说,您想要查看其中之一,而不是两者,当然也不是作为单个字符串。当您说“DTD 被请求为格式的 URI”时,不清楚是谁在发出请求。看起来调用代码连接了 SYSTEM 和 PUBLIC。

如果您将 DTD 作为磁盘文件,并且您所需要做的就是将一个 URI 映射到另一个 URI,则可以重写 ResolveUri() 而不是完整的 GetEntity()。如果您拥有无法作为简单 URI 访问的资源,例如您在运行时计算资源的内容、您从数据库中获取它、您使用非标准 URL 方案,则 GetEntity() 会更有用以及像 svn: 等协议。

"file:///absolut/path/to.xml/-//OWNER//NAME//EN" is a concatenation of the SYSTEM and the PUBLIC identifiers. Generally, you want to look at one or the other, not both, and certainly not as a single string. When you say "DTD is requested as a URI of the format," it is not clear who is doing the requesting. It appears that the calling code concatenates SYSTEM and PUBLIC.

If you have the DTD as a disk file and all you need to do is map one URI to another, you can override ResolveUri() instead of the full GetEntity(). GetEntity() is more useful if you have resources that are inaccessible as straightforward URIs, e.g. you compute the content of the resource at runtime, you fetch it from a database, you use a non-standard URL scheme and protocol like svn: etc.

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