2017-09-14 109 views
0

我已經知道如何驗證一個單一的xsd與PHP程序,我仍然有一個問題標籤在xsd驗證時,我按照本教程https://www.tutorialspoint.com/xsd/xsd_complex_any.htm,以下是我的演示:如何驗證xml與php當我有多個架構文件

person.xsd

<?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema 
    xmlns="http://cos-git.dev" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://cos-git.dev" 
    elementFormDefault="qualified"> 

    <xs:element name="persons"> 
     <xs:complexType> 
     <xs:sequence> 
      <xs:element name="person" maxOccurs="unbounded"> 
      <xs:complexType> 
       <xs:sequence> 
       <xs:element name="full_name" type="xs:string"/> 
       <xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="5"/> 
       <xs:any minOccurs = "0"/> 
       </xs:sequence> 
      </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    </xs:schema> 

和我child.xsd

<?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema 
     xmlns="http://www.w3school.com.cn" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     targetNamespace="http://www.w3school.com.cn" 
     elementFormDefault="qualified"> 

     <xs:element name="children"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="childname" type="xs:string" 
       maxOccurs="unbounded"/> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
    </xs:schema> 

和我的test.xml文件如下:

<persons xmlns="http://cos-git.dev" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://cos-git.dev person.xsd http://www.w3school.com.cn child.xsd"> 
    <person> 
     <full_name>Tony Smith</full_name> 
     <child_name>Cecilie</child_name> 
     <children> 
      <childname>Name</childname> 
     </children> 
    </person> 
</persons> 

單驗證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(); 
    var_dump($errors);exit; 
    foreach ($errors as $error) { 
     print libxml_display_error($error); 
    } 
    libxml_clear_errors(); 
} 

// Enable user error handling 
libxml_use_internal_errors(true); 

$xml = new DOMDocument(); 
$xml->load('xmlfilepath'); 

if ($xml->schemaValidate('xsdfilename')) { 
    echo "validated</n>"; 

}else{ 
    libxml_display_errors(); 
} 


?> 

現在我的問題是如何驗證的test.xml與person.xsd和child.xsd。你能給我一些提示嗎?

==============

感謝CM Sperberg - 麥昆給我一些提示,以創建另一個文件名爲driver.xsd解決我的問題

的以下是我的解決方案:

driver.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:import namespace="http://cos-git.dev" schemaLocation="person.xsd"/> 
<xs:import namespace="http://www.w3school.com.cn" schemaLocation="child.xsd"/> 
</xs:schema> 

和我的test.xml

<?xml version="1.0" encoding="UTF-8"?> 

<persons xmlns="http://cos-git.dev" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:r="http://www.w3school.com.cn"> 
    <person > 
     <full_name>Tony Smith</full_name> 
     <child_name>Cecilie</child_name> 
     <r:children > 
      <r:childname>Name</r:childname> 
      <r:childname>Name1</r:childname> 
     </r:children> 
    </person> 
</persons> 

現在我可以通過幾個XSD具有相同的PHP驗證計劃驗證我的XML

非常感謝

如果您有其他的解決方案,請分享你的想法:)

回答

1

如果我理解正確的,你的問題相當於:如何讓我的XSD驗證程序加載(並構建出一個模式)person.xsd和child.xsd?答案原則上取決於你的驗證者;檢查它的文檔。許多驗證器將允許您在調用它們時指定多個模式文檔。

如果您找不到可以回答您的問題的模式驗證程序的文檔,或者您的驗證程序在運行時只願意接受單個模式文檔(或此類URI),那麼最簡單的做法是讓所謂的模式文檔driver.xsd其內容會是這樣的(未測試):

<xsd:schema ...> 
    <xsd:import namespace="http://cos-git.dev" 
    schemaLocation=".../person.xsd"/> 
    <xsd:import namespace="http://www.w3school.com.cn" 
    schemaLocation=".../child.xsd"/> 
</xsd:schema> 

當你驗證對driver.xsd架構文檔,你會被告知,複合型的「人」是非確定性的:因爲您的通配符匹配'child_name'元素,輸入中的'child_name'元素可以匹配該名稱的本地元素或者未聲明的全局元素名字。解析器不會猜測是什麼意思。修復該問題最簡單的方法是將namespace="##other"添加到通配符。

+0

非常感謝您的建議,我似乎找到了解決方案。你能否幫我檢查一下我已經參加過我的問題的解決方案 –