Search code examples
phpxmlmultiple-schema

Is it possible to validate XML against multiple schemas in PHP?


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!


Solution

  • 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.