使用PHP的schemaValidate无效的架构

时间:2013-01-10 14:29:08

标签: php

我正在尝试使用PHP验证特定XSD的xml文件。使用EasyPHP-12.1(PHP 5.4.6版)在Windows 7上进行了测试。这是服务器端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>

2 个答案:

答案 0 :(得分: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>"

答案 1 :(得分: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)