PHP | DOMDocument schemaValidate() Function Last Updated : 20 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMDocument::schemaValidate() function is an inbuilt function in PHP which is used to validate a document based on the given schema file. The schema file can be in an XSD format which is the recommendation from W3C (World Wide Web Consortium). Syntax: bool DOMDocument::schemaValidate( string $filename, int $flags = 0 ) Parameters: This function accept two parameters as mentioned above and described below: $filename: It specifies the path to the schema. $flags (Optional): It specifies the validation flags. Return Value: This function returns TRUE on success or False on failure. Below given programs illustrate the DOMDocument::schemaValidate() function in PHP: Program 1: File name: rule.xsd php <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="student"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="rollno" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> File name: index.php php <?php // Create a new DOMDocument $doc = new DOMDocument; // Load the XML $doc->loadXML("<?xml version=\"1.0\"?> <student> <name>Rahul </name> <rollno>34</rollno> </student>"); // Check if XML follows the rule if ($doc->schemaValidate('rule.xsd')) { echo "This document is valid!\n"; } ?> Output: This document is valid! Program 2: File name: rule.xsd php <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="body"> <xs:complexType> <xs:sequence> <xs:element name="h1" type="xs:string"/> <xs:element name="strong" type="xs:integer"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> File name: index.php php <?php // Create a new DOMDocument $doc = new DOMDocument; // Load the XML $doc->loadXML("<?xml version=\"1.0\"?> <student> <h1>Rahul </h1> </student>"); // Check if XML follows the rule if (!$doc->schemaValidate('rule.xsd')) { echo "This document is not valid!\n"; } ?> Output: This document is not valid! Reference: https://www.php.net/manual/en/domdocument.schemavalidate.php Comment More infoAdvertise with us Next Article PHP | DOMDocument schemaValidate() Function gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMDocument schemaValidateSource() Function The DOMDocument::schemaValidateSource() function is an inbuilt function in PHP which is used to validate a document based on a schema defined in the given string. The difference between schemaValidate() and schemaValidateSource() is that the former accepts a schema filename whereas latter can accept 2 min read PHP | DOMDocument validate() Function 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 fa 2 min read PHP | DOMDocument relaxNGValidate() Function The DOMDocument::relaxNGValidate() function is an inbuilt function in PHP which is used to performs relaxNG validation on the document. The relaxNG is an alternative to DDT and defines a structure which needs to be followed by the XML document. Syntax: bool DOMDocument::relaxNGValidate( string $file 2 min read PHP | DOMDocument save() Function The DOMDocument::save() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after creating the new dom document from scratch. Syntax: int DOMDocument::save( string $filename, int $options = 0 ) Parameters: This function ac 2 min read PHP | DOMDocument relaxNGValidateSource() Function The DOMDocument::relaxNGValidateSource() function is an inbuilt function in PHP which is used to perform relaxNG validation on the document using a string as RNG schema. The difference between relaxNGValidate() and relaxNGValidateSource() is that the former accepts a rng schema filename whereas latt 2 min read Like