![](/img/trans.png)
[英]Element 'foo': This element is not expected". Expected is one of ( {url}foo, {url}bar )
[英]Element 'foo': This element is not expected. Expected is ( {http://www.example.com}foo )
當嘗試使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.