Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SimpleXMLElement validation with XSD

is it possible to validate a SimpleXMLElement with an XSD shema stored in a string?

I get this xml trough CURL:

<production_feedback>
        <production_number>DA1100208</production_number>
    <production_status>DONE</production_status>
</production_feedback>

On my side i get it like this:

if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){

    $post_text = file_get_contents('php://input');

    $xml = new SimpleXMLElement($post_text);

    error_log(print_r($xml , true));
    }

This is in my error_log() :

SimpleXMLElement Object\n(\n    [production_number] => DA1100208\n    [production_status] => PRODUCTION_IN_PROGRESS\n)\n

So i can access the data with Xpath. This works well. I would like to validate it with an XSD. Is it possible, or is there any other way 2 validate the XML string with the XSD string?

this is my XSD btw stored in a variable:

$production_XSD ='<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="production_feedback">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="production_number"/>
        <xs:element type="xs:string" name="production_status"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>'
like image 530
Barta Tamás Avatar asked Sep 20 '12 06:09

Barta Tamás


1 Answers

The SimpleXMLElement class doesn't support that (as far as the documentation on php.net is up to date).

The DOMDocument provides the functionality you're looking for using the DOMDocument::schemaValidateSource method.

---- Original

The XMLReader class however has the setSchema method which can be used for validation against a XSD file (It's not exactly what you were looking for, but that's what I found without relying on any external libraries)

like image 120
Louis Huppenbauer Avatar answered Nov 16 '22 19:11

Louis Huppenbauer