我已經知道如何驗證一個單一的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
非常感謝
如果您有其他的解決方案,請分享你的想法:)
非常感謝您的建議,我似乎找到了解決方案。你能否幫我檢查一下我已經參加過我的問題的解決方案 –