views:

37

answers:

2

Hi,

I'm wondering if it is possible to validate xml against multiple schemas in PHP or I have to merge my schemas somehow.

Thanks for an answer!

A: 

Considering that the DOMDocument::schemaValidate method receives the path to the schema file as a parameter, I'd say that you just have to call that method several times : once for each one of your schemas.

See also DOMDocument::schemaValidateSource if you have your schemas in PHP strings ; the idea (and the answer) will be the same, though : just call the method several times.

Pascal MARTIN
It was also my guess but it doesn't work unfortunately. I guess I will have to merge my schemas.
MartyIX
A: 

I've solved my problem via simple PHP script:

$mainSchemaFile = dirname(__FILE__) . "/main-schema.xml";
$additionalSchemaFile = 'second-schema.xml';


$additionalSchema = simplexml_load_file($additionalSchemaFile);
$additionalSchema->registerXPathNamespace("xs", "http://www.w3.org/2001/XMLSchema");
$nodes = $additionalSchema->xpath('/xs:schema/*');    

$xml = '';  
foreach ($nodes as $child) {
  $xml .= $child->asXML() . "\n";
}

$result = str_replace("</xs:schema>", $xml . "</xs:schema>", file_get_contents($mainSchemaFile));

var_dump($result); // merged schema in form XML (string)

But it is possible only thanks to the fact that the schemas are the same - i.e.

<xs:schema xmlns="NAMESPACE"
           targetNamespace="NAMESPACE"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

is in both files.

MartyIX