针对 PHP 中给定 DTD 的 XML 验证
在 PHP 中,我尝试使用应用程序指定的 DTD(而不是外部获取的 XML 文档)来验证 XML 文档。 DOMDocument 类中的 validate 方法似乎仅使用 XML 文档本身指定的 DTD 进行验证,因此这不起作用。
这可以完成吗?如何完成?或者我是否必须将 DTD 转换为 XML 模式,以便可以使用 schemaValidate 方法?
(这似乎在 在 PHP 中使用自定义 DTD 验证 XML 中被问到 但没有正确答案,因为该解决方案仅依赖于目标 XML 指定的 DTD)
In PHP, I am trying to validate an XML document using a DTD specified by my application - not by the externally fetched XML document. The validate method in the DOMDocument class seems to only validate using the DTD specified by the XML document itself, so this will not work.
Can this be done, and how, or do I have to translate my DTD to an XML schema so I can use the schemaValidate method?
(this seems to have been asked in Validate XML using a custom DTD in PHP but without correct answer, since the solution only relies on DTD speicified by the target XML)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:XML 验证可能会受到 Billion Laughs 攻击和类似的 DoS 向量。
这本质上就是 rojoca 在他的评论中提到的:
这将根据
bar.dtd
验证文档。您不能只调用
$new->loadXML()
,因为这只会将 DTD 设置为原始数据,并且读取 DOMDocument 对象的doctype
属性-only,因此您必须将根节点(及其中的所有内容)复制到新的 DOM 文档。我自己只是尝试了一下,所以我不完全确定这是否涵盖了所有内容,但它绝对适用于我的示例中的 XML。
当然,快速而肮脏的解决方案是首先以字符串形式获取 XML,搜索并用您自己的 DTD 替换原始 DTD,然后加载它。
Note: XML validation could be subject to the Billion Laughs attack, and similar DoS vectors.
This essentially does what rojoca mentioned in his comment:
This will validate the document against the
bar.dtd
.You can't just call
$new->loadXML()
, because that would just set the DTD to the original, and thedoctype
property of a DOMDocument object is read-only, so you have to copy the root node (with everything in it) to a new DOM document.I only just had a go with this myself, so I'm not entirely sure if this covers everything, but it definitely works for the XML in my example.
Of course, the quick-and-dirty solution would be to first get the XML as a string, search and replace the original DTD by your own DTD and then load it.
我认为这只能通过 XSD 实现,请参阅:
http://php.net/manual/en/domdocument.schemavalidate#62032
I think that's only possible with XSD, see:
http://php.net/manual/en/domdocument.schemavalidate#62032