簡體   English   中英

使用XSD進行XML驗證

[英]XML validation with 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 :(我將其放入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

我需要重寫架構,以便它會注意到main元素內的* many元素(請注意相同的名稱用法)。

我已經使用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>

您可以從上面開始進行調整。

驗證結果:

驗證結果

如果您希望手動修復您創建的工具的XSD,請確保catalogType的前兩次出現重命名為其他名稱(例如, catalogType1 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM