Validate XML against XSD 1.1 schema in PHP

Viewed 327

As stated by the official PHP documentation:

DOMDocument::schemaValidate — Validates a document based on a schema. Only XML Schema 1.0 is supported.

That being said, I have an XML Schema 1.1 file that I need to validate my XML against. My search for a solution has turned out nothing so far and judging by the answers on similar questions, libxml seem to only support 1.0, so anything that uses libxml is probably not going to work in this case.

Edit: For extra information, the schema contains the following (which is only valid for XML 1.1 and not for 1.0):

  <xs:element name="placeholder_1">
    <xs:complexType>
      <xs:all>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="placeholder_2" type="xs:string"/>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="placeholder_3" type="xs:string"/>
        <xs:element maxOccurs="1" minOccurs="0" name="placehoder_4" type="xs:string"/>
        <xs:element maxOccurs="1" minOccurs="0" name="placeholder_5" type="xs:string"/>
      </xs:all>
    </xs:complexType>
  </xs:element>
1 Answers

Can you try as said below and see if it is working?

Convert the schema file to string.

Can be done roughly as said below (may be better ways available)

$doc->load('schema.xsd');
$doc->save('schema.xml');
$xmlfile = file_get_contents('schema.xml');

And then use DOMDocument::schemaValidateSource which takes string as input.

https://www.php.net/manual/en/domdocument.schemavalidatesource.php

Validates a document based on a schema defined in the given string.

Related