![](/img/trans.png)
[英]How to disable warning produced by DOMDocument::schemaValidate() when the schema is invalid?
[英]Invalid Schema using schemaValidate of PHP
我正在嘗試使用PHP針對特定的XSD驗證xml文件。 已在Windows 7上使用EasyPHP-12.1(PHP 5.4.6版)進行了測試。 這是服務器端PHP文件,該文件加載xml,對其進行驗證並顯示任何錯誤。
PHP代碼
function libxml_display_error($error)
{
$return = "<br/>\n";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "<b>Warning $error->code</b>: ";
break;
case LIBXML_ERR_ERROR:
$return .= "<b>Error $error->code</b>: ";
break;
case LIBXML_ERR_FATAL:
$return .= "<b>Fatal Error $error->code</b>: ";
break;
}
$return .= trim($error->message);
if ($error->file) {
$return .= " in <b>$error->file</b>";
}
$return .= " on line <b>$error->line</b>\n";
return $return;
}
function libxml_display_errors() {
$errors = libxml_get_errors();
foreach ($errors as $error) {
print libxml_display_error($error);
}
libxml_clear_errors();
}
// Enable error handling
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml->load('testFile');
if (!$xml->schemaValidate('sampleSchema.xsd')) {
print '<b>Errors Found!</b>';
libxml_display_errors();
}
else {
echo "Validated Successfully!";
}
使用下面的xml和xsd模式,它可以成功運行而不會產生任何錯誤:
XML:
<lures>
<lure>
<lureName>Silver Spoon</lureName>
<lureCompany>Clark</lureCompany>
<lureQuantity>7</lureQuantity>
</lure>
</lures>
XSD:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="lures">
<xs:complexType>
<xs:sequence>
<xs:element name="lure">
<xs:complexType>
<xs:sequence>
<xs:element name="lureName" type="xs:string"/>
<xs:element name="lureCompany" type="xs:string"/>
<xs:element name="lureQuantity" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
但是,當我嘗試根據標准Mpeg7架構文件mpeg7-v1.xsd驗證Mpeg7 xml時,會出現問題,其中PHP生成以下錯誤:
Warning: DOMDocument::schemaValidate(): Invalid Schema in C:\Program Files (x86)\EasyPHP-12.1\www\ServerSideTests\validate.php on line 38
Errors Found!
Error 3008: local list type: A type, derived by list or union, must have the simple ur-type definition as base type, not '{urn:mpeg:mpeg7:schema:2001}integerVector'. in file:///C:/Program%20Files%20(x86)/EasyPHP-12.1/www/ServerSideTests/mpeg7-v1.xsd on line 1251
我不知道怎么可能使正式的XSD無效...由於XSD完全正確,那么libxml庫可能與此W3C Schema不完全兼容嗎? 我只想知道如何修改上面的代碼以成功驗證mpeg7文件,或者是否必須將其實現為另一種服務器端語言...
編輯:我的測試Mpeg7文件
<Mpeg7 xmlns="urn:mpeg:mpeg7:schema:2001"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mpeg7="urn:mpeg:mpeg7:schema:2001"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xsi:schemaLocation="urn:mpeg:mpeg7:schema:2001 mpeg7-v1.xsd">
<Description xsi:type="CompleteDescriptionType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Relationships/>
</Description>
</Mpeg7>
我認為XML需要
<?xml version="1.0" encoding="utf-8"?>
<lures xmlns="<THE NAME SPACE HERE"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://<SCHEMA NS LOCARTION - i.e. unique> lures\">
然后XSD需要
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="<SCHEMA NS LOCATION - as above>"
XML格式
<?xml version="1.0" encoding="utf-8"?>
<lures xmlnshttp://nonwhere.com/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://nowhere.com lures\">
<lures>
<lure>
<lureName>Silver Spoon</lureName>
<lureCompany>Clark</lureCompany>
<lureQuantity>7</lureQuantity>
</lure>
</lures>
XSD
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://nowhere.com">
<xs:element name="lures">
<xs:complexType>
<xs:sequence>
<xs:element name="lure">
<xs:complexType>
<xs:sequence>
<xs:element name="lureName" type="xs:string"/>
<xs:element name="lureCompany" type="xs:string"/>
<xs:element name="lureQuantity" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
似乎為我工作。 (即PHP)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.