I have already known how to validate a single xsd with php program , I still have a problem with <xs:any>
label in xsd validation when I follow this tutorial https://www.tutorialspoint.com/xsd/xsd_complex_any.htm , the following is my demo:
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>
and my 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>
and my test.xml file is as follow:
<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>
the single validate php program is as follow:
<?php
function libxml_display_error($error)
{
$return = "<br/>
";
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>
";
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();
}
?>
Now My problem is that how to validate test.xml with person.xsd and child.xsd . Could you give me some tips?
==============
Thanks for C. M. Sperberg-McQueen to give me some tips to create another file named driver.xsd to solve my problem
The following is my solution:
<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>
and my 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>
now I can validate my xml by several xsd with same php validation program
Thanks a lot
If you have another solution please share your idea :)
If I understand correctly, your question amounts to: how do I get my XSD validator to load (and build a schema out of) both person.xsd and child.xsd? The answer will in principle depend on your validator; check its documentation. Many validators will allow you to specify multiple schema documents at the time you call them.
If you cannot find documentation for your schema validator that answers your question, or if your validator is only willing to accept a single schema document (or URI for such) at run time, then the simplest thing to do would be to make a schema document called driver.xsd
whose contents will be something like this (not tested):
<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>
When you do validate against the driver.xsd schema document, you will be told that the complex type for 'person' is non-deterministic: because your wildcard matches the 'child_name' element, a 'child_name' element in the input could match either the local element of that name or an undeclared global element of that name. The parser won't be willing to guess which is meant. The simplest way to repair that problem will be to add namespace="##other"
to the wildcard.
I have an idea about this, and i am working in it. First, the xml must have the schemaLocation for all xsd required files. We need read the xml and detect all xsd required files. validate the XML using an unique xsd (the main or first), but importing in it (the base xsd) the rest of reqired xsd. I solve this using C#, but now i need too in PHP.