PHP | DOMDocument schemaValidate() Function
Last Updated :
20 Feb, 2020
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
<?xml version= "1.0" ?>
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
$doc = new DOMDocument;
$doc ->loadXML("<?xml version=\"1.0\"?>
<student>
<name>Rahul </name>
<rollno>34</rollno>
</student>");
if ( $doc ->schemaValidate( 'rule.xsd' )) {
echo "This document is valid!\n" ;
}
?>
|
- Output:
This document is valid!
Program 2:
Reference: https://www.php.net/manual/en/domdocument.schemavalidate.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...