的schemaValidate我試圖驗證XML文件對用PHP特定的XSD。使用EasyPHP-12.1(PHP 5.4.6版)在Windows 7上進行了測試。這裏是加載xml的服務器端PHP文件,驗證它並顯示任何錯誤。無效的架構使用PHP
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:
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
我不知道,怎麼可能是無效的:
然而,當我嘗試按標準MPEG-7架構文件mpeg7-v1.xsd,其中PHP生成以下錯誤來驗證MPEG-7的XML出現問題官方XSD ......因爲XSD是完全正確的,那麼也許libxml的庫是不是與此W3C架構完全兼容? 所有我想知道的是我怎麼也得修改上面的代碼爲MPEG-7文件,或者如果我必須落實到另一臺服務器端語言的succesfull驗證...
編輯:我的測試MPEG-7文件
<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和XSD我提供上面它們沒有問題驗證。問題是,PHP無法驗證標準mpeg7-v1.xsd而xml格式正確 – KostKont