【问题标题】:Schema validation failed php [closed]架构验证失败的php [关闭]
【发布时间】:2013-03-04 18:24:24
【问题描述】:

我创建了DOMDocument元素,多次调用appendChild()等等。

在我完成构建 XML 后,我想validate it against an XSD schema。

$newDocument->schemaValidate($schemaPath);

我有错误

No matching global declaration available for the validation root. 

但是,如果我将生成的 XML 保存到文件中,请打开它并验证一切正常。或者,如果执行以下操作:

$newDocument->loadXML($newDocument->saveXML());

然后一切都很好。

你能解释一下,为什么验证器在第一种情况下找不到根元素?

更新

我如何构建我的 xml:

$newDocument = new DOMDocument();

$rootElement = $newDocument->createElement('ONIXMessage');

$rootElement->setAttribute('xmlns', 'http://www.editeur.org/onix/2.1/reference');

$newDocument->appendChild($rootElement);

在我将子元素添加到根元素后,但当我尝试根据 XML 架构对其进行验证时,即使列出的代码也会产生错误。

关于 xml 架构文件。我从 EDItEUR 组织下载了它,所以我相信问题就在我这边。链接到网站上的 .xsd 文件http://www.editeur.org/onix/2.1/reference/ONIX_BookProduct_Release2.1_reference.xsd

【问题讨论】:

标签: php xml xsd domdocument


【解决方案1】:

据我所知,一些旧版本的 libxml 的人会遇到这个问题,它与创建元素的命名空间有关。

也许您可以尝试使用createElementNS() 而不是简单的createElement(然后是appendChild)创建元素,并指定与架构文件相同的NS。

【讨论】:

    【解决方案2】:

    我认为您应该使用createElementNS 而不是createElement - 即:

    $newDocument = new DOMDocument();
    $rootElement = $newDocument->createElementNS('http://www.editeur.org/onix/2.1/reference', 'ONIXMessage');
    $newDocument->appendChild($rootElement);
    

    发生的情况是内存中的DOMDocument 在 null 命名空间中有根元素 - 因此验证失败,但它有命名空间声明 - 所以当它被保存并重新加载时,根元素结束在正确的命名空间中,验证工作正常。

    【讨论】:

      猜你喜欢
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2015-02-01
      相关资源
      最近更新 更多