2013-01-10 35 views
0

的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> 

回答

0

我認爲,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>" 
+0

正如我所說,XML和XSD我提供上面它們沒有問題驗證。問題是,PHP無法驗證標準mpeg7-v1.xsd而xml格式正確 – KostKont

0

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)

+0

我已經說過,xml和xsd u正在嘗試對我來說也工作得很好。問題是與我原來的編輯的XML當我嘗試驗證其對MPEG-7-v1.xsd。 – KostKont

+0

然後提供XSD!不是您給我們的XSD。 –

+0

只需點擊MPEG-7-v1.xsd在我原來的職位,它鏈接到XSD。 – KostKont