当尝试使用schemaValidate方法针对某个架构验证PHP DOMDocument对象时,将生成下一个警告:
警告:DOMDocument :: schemaValidate():元素'foo':不需要该元素。预期为({ http://www.example.com } foo)。在Y行的X中
它仅在附加到DOMDocument的元素上发生。我准备了下一个代码段和架构,以便任何人都可以立即进行测试:
片段:
$template = '
<root
xmlns="http://www.example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com schema.xsd"
>
<bar/>
</root>
';
$DD = new DOMDocument();
$DD -> loadXML($template);
$foo = $DD -> createElement('foo');
$DD -> getElementsByTagName('root') -> item(0) -> appendChild($foo);
var_dump(htmlentities($DD -> saveXML()));
var_dump($DD -> schemaValidate(__DIR__ . '/schema.xsd'));
架构:
<?xml version="1.0"?>
<xs:schema
targetNamespace="http://www.example.com"
xmlns:SiiDte="http://www.example.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="bar"/>
<xs:element name="foo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我没有看到foo和bar之间的区别,除了foo是通过appendChild方法添加的,而bar是通过loadXML方法添加的。
验证返回false(这意味着验证错误)。当使用loadXML方法加载foo时,错误停止发生,但这绝对不是解决方案,因为在很多情况下需要动态创建XML。
¿为什么附加元素会产生此验证错误,如何解决?
<foo>
您创建的元素“缺少”名称空间,因此在空名称空间中。
命名空间也是错误消息中大括号(或尖括号)中显示的部分:
{http://www.example.com}foo
`----------------------´`-´
namespace name
而不是createElement
使用createElementNS
元素名称旁边的名称空间。
当您将创建的文档另存为XML(例如通过查看对其进行手动验证)时,您完全可以认为该元素看起来像<bar>
:
<?xml version="1.0"?>
<root xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com schema.xsd">
<bar/>
<foo/></root>
但是它只是添加了一个空的namspace(因此插入时没有太多的名称空间),并且在内存中该元素仍然没有名称空间-并且验证在内存中。
这是一个实际的验证示例:
<?php
/**
* Element 'foo': This element is not expected. Expected is ( {http://www.example.com}foo )
*
* @link http://stackoverflow.com/a/29925747/367456
*/
$template = <<<XML
<root
xmlns="http://www.example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com schema.xsd">
<bar/>
</root>
XML;
$schema = <<<XML
<?xml version="1.0"?>
<xs:schema
targetNamespace="http://www.example.com"
xmlns:SiiDte="http://www.example.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="bar"/>
<xs:element name="foo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML;
$schema = 'data://text/xml;base64,' . base64_encode($schema);
$namespace = 'http://www.example.com';
$doc = new DOMDocument();
$doc->loadXML($template);
$foo = $doc->createElementNS($namespace, 'foo');
$doc->documentElement->appendChild($foo);
echo $doc->saveXML();
var_dump($doc->schemaValidate($schema));
输出为:
<?xml version="1.0"?>
<root xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com schema.xsd">
<bar/>
<foo/></root>
bool(true)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句