Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do XSD validation of an XML stream in PHP

Let's say I want to be able to validate that a 50GB+ XML file conforms to a given XSD. I could use

DOMDocument::load & DOMDocument::schemaValidate

but that will take all of time on loading and will generally exhaust all available memory for me. Is there any way to feed an XSD to a SAX or any other type of stream processor and have it verify that all is well?

like image 667
tomasz Avatar asked Sep 09 '09 21:09

tomasz


People also ask

How validate XML in PHP?

The DOMDocument::validate() function is an inbuilt function in PHP which is used to validate the document based on its DTD (Document Type Definition). DTD defines the rules or structure to be followed by the XML file and if a XML document doesn't follows this format then this function will return false.

Can we validate XML documents against a schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.

What is XSD validator?

The WSRR web user interface validates each definition file when starting, and when new or updated definition files are loaded. This is done according to the definition XML Schema Definition (XSD).


1 Answers

You may use XMLReader:

$reader = new XMLReader();
$reader->open('xmlfile.xml');
$reader->setSchema('schemafile.xsd');
while($reader->read());
$reader->close();
like image 159
Heri Avatar answered Oct 20 '22 11:10

Heri