Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | XMLReader setSchema() Function

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 )




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

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




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

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




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

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

Example 2:

  • data.xml




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

    
    
  • rule.xsd




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

    
    
  • index.php




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

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

Reference: https://www.php.net/manual/en/xmlreader.setschema.php

RELATED ARTICLES

Most Popular

Recent Comments