2012-03-29 170 views
1

我有我想驗證的這種XML產品。使用XSD進行XML驗證

(我把它放在xml.xml文件)

<?xml version="1.0" encoding="UTF-8"?> 
<catalog label="global"> 
    <catalog label="animals" link="/animals.php"> 
    <subject>Subject1</subject> 
    </catalog> 
    <catalog label="animals" link="/animals.php"> 
    <subject>Subject2</subject> 
    </catalog> 
</catalog> 

生成的XSD與this tool:(我把文件xml.xsd)

<?xml version="1.0" encoding="utf-8"?> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="catalog" type="catalogType" /> 
    <xsd:complexType name="catalogType"> 
    <xsd:sequence> 
     <xsd:element maxOccurs="unbounded" name="catalog" type="catalogType" /> 
    </xsd:sequence> 
    <xsd:attribute name="label" type="xsd:string" /> 
    </xsd:complexType> 
    <xsd:complexType name="catalogType"> 
    <xsd:sequence> 
     <xsd:element name="subject" type="xsd:string" /> 
    </xsd:sequence> 
    <xsd:attribute name="label" type="xsd:string" /> 
    <xsd:attribute name="link" type="xsd:string" /> 
    </xsd:complexType> 
</xsd:schema> 

和我使用的PHP代碼測試它們(在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 checkXMLStructure($xml_object) 
    { 
     $xmlElement = $xml_object->catalog; 
     $dom_sxe = dom_import_simplexml($xmlElement); 

     $dom = new DOMDocument('1.0'); 
     $dom_sxe = $dom->importNode($dom_sxe, true); 
     $dom_sxe = $dom->appendChild($dom_sxe); 

     if (!$dom->schemaValidate(dirname(__FILE__) . '/xml.xsd')) { 
      echo "BAD XML\n"; 
     return; 
     } 

     echo "*GOOD* XML!\n"; 
    } 

// Enable user error handling 
libxml_use_internal_errors(true); 

$xml_object = simplexml_load_file('xml.xml'); 
checkXMLStructure($xml_object); 

// Display Errors 
$errors = libxml_get_errors(); 
foreach ($errors as $error) { 
print libxml_display_error($error); 
} 
libxml_clear_errors(); 

輸出:

PHP Warning: DOMDocument::schemaValidate(): Element '{http://www.w3.org/2001/XMLSchema}complexType': A global complex type definition 'catalogType' does already exist. in /path/php.php on line 13 
PHP Stack trace: 
PHP 1. {main}() /path/php.php:0 
PHP 2. checkXMLStructure() /path/php.php:23 
PHP 3. DOMDocument->schemaValidate() /path/php.php:13 
PHP Warning: DOMDocument::schemaValidate(): Invalid Schema in /path/php.php on line 13 
PHP Stack trace: 
PHP 1. {main}() /path/php.php:0 
PHP 2. checkXMLStructure() /path/php.php:23 
PHP 3. DOMDocument->schemaValidate() /path/php.php:13 
BAD XML 

我需要重寫模式,以便它會注意到主元素中的*許多元素(請注意相同的名稱用法)。

+1

問題是? – 2012-03-29 20:32:31

+0

對不起,在問題中更新。 – valk 2012-03-29 20:43:05

+0

還是不知道問題是什麼 – eddy147 2016-07-25 13:51:54

回答

1

我用QTAssistant產生下面的架構(這將驗證您所提供的XML):

<?xml version="1.0" encoding="utf-8"?> 
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)--> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="catalog"> 
    <xsd:complexType> 
     <xsd:sequence> 
     <xsd:element maxOccurs="unbounded" name="catalog"> 
      <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="subject" type="xsd:string" /> 
      </xsd:sequence> 
      <xsd:attribute name="label" type="xsd:string" use="required" /> 
      <xsd:attribute name="link" type="xsd:string" use="required" /> 
      </xsd:complexType> 
     </xsd:element> 
     </xsd:sequence> 
     <xsd:attribute name="label" type="xsd:string" use="required" /> 
    </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

你可以從上面開始並調整它,你走。

驗證結果:

Validation results

如果你想手動修復您的工具創建的XSD,然後確保你重命名的前兩個catalogType的出現到別的東西(如catalogType1 )。

+0

感謝XSD朋友,但與PHP代碼runnint上面仍然會產生錯誤:PHP php.php BAD XML
錯誤1866年:元素「目錄」,屬性「鏈接':屬性'link'是不允許的。 on line
錯誤1871:元素'subject':該元素不是預期的。預計是(目錄)。在線 – valk 2012-03-29 21:57:20

+0

嗨@valk,我附上您發佈的XML和我生成的XSD的屏幕截圖;確認在輸出面板中顯示爲OK。你一定還有其他問題(可能是你使用的XML)?你確定你剛剛嘗試過的XML與發佈的XML相同嗎?我想不出其他什麼:(... – 2012-03-29 22:05:53

+0

以及我很確定,現在我重新檢查它。它可能是$ dom-> schemaValidate()有一些錯誤,但我懷疑它。 5.3,但它不應該有所作爲 – valk 2012-03-29 22:16:30