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
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • File name: index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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";
    }
    ?>

    chevron_right

    
    

  • Output:
    This document is valid!

Program 2:

  • File name: rule.xsd
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • File name: index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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";
    }
    ?>

    chevron_right

    
    

  • Output:
    This document is not valid!

Reference: https://www.php.net/manual/en/domdocument.schemavalidate.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.