PHP | XMLReader setSchema() Function

The XMLReader::setSchema() function is an inbuilt function in PHP which is used to validate the document using a XSD schema. This XSD schema is nothing but a file that defines the rule for the XML structure and should be in the form of .xsd file.

Syntax:

bool XMLReader::setSchema( string $filename )

Parameters: This function accepts a single parameter $filename which holds the XSD filename.

Return Value: This function returns TRUE on success or FALSE on failure.

Exceptions: This function throws E_WARNING if libxml extension was compiled without schema support.



Below examples illustrate the XMLReader::setSchema() function in PHP:

Example 1:

  • data.xml ( The XML file to be validated )
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0"?>
    <student>
        <name>Rahul</name>
        <rollno>1726262</rollno>
    </student>

    chevron_right

    
    

  • rule.xsd ( The rules to be followed by the XML file )
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0"?>
    <xs:schema xmlns:xs=
    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

    
    

  • index.php ( The PHP script to run the validation )
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Open the XML file
    $XMLReader->open('data.xml');
      
    // Load the rule
    $XMLReader->setSchema('rule.xsd');
      
    // Iterate through the XML nodes
    while ($XMLReader->read()) {
        if ($XMLReader->nodeType == XMLREADER::ELEMENT) {
      
            // Check if XML follows the XSD rule
            if ($XMLReader->isValid()) {
                echo "This document is valid!<br>";
            }
      
        }
    }
    ?>

    chevron_right

    
    

  • Output:
    This document is valid!
    This document is valid!
    This document is valid!

Example 2:

  • data.xml
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0"?>
    <div>
        <phoneNo>This should not be text</phoneNo>
    </div>

    chevron_right

    
    

  • rule.xsd
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0"?>
    <xs:schema xmlns:xs=
    elementFormDefault="qualified">
        <xs:element name="div">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="phoneNo"
                       type="xs:integer"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>

    chevron_right

    
    

  • index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Open the XML file
    $XMLReader->open('data.xml');
      
    // Load the rule
    $XMLReader->setSchema('rule.xsd');
      
    // Iterate through the XML nodes
    while ($XMLReader->read()) {
        if ($XMLReader->nodeType == XMLREADER::ELEMENT) {
      
            // Check if XML follows the XSD rule
            if (!$XMLReader->isValid()) {
                echo "This document is not valid!<br>";
            }
      
        }
    }
    ?>

    chevron_right

    
    

  • Output:
    This document is not valid!
    This document is not valid!

Reference: https://www.php.net/manual/en/xmlreader.setschema.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.