Wednesday, June 17, 2026
HomeLanguagesPHP | DOMDocument schemaValidate() Function

PHP | DOMDocument schemaValidate() Function

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"?>
    <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
      
    // 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




    <?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
      
    // 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

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS