Saturday, November 22, 2025
HomeLanguagesPHP | XMLReader setRelaxNGSchema() Function

PHP | XMLReader setRelaxNGSchema() Function

The XMLReader::setRelaxNGSchema() function is an inbuilt function in PHP which is used to set the filename or URI for the RelaxNG Schema to use for validation.

Syntax:

bool XMLReader::setRelaxNGSchema( string $filename )

Parameters: This function accepts a single parameter $filename which holds the filename or URI pointing to a RelaxNG Schema.

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

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

    Example 1:

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




    <?xml version="1.0"?>
    <body>
        <div>
            <h1>Heading 1</h1>
            <h2>Heading 2</h2>
        </div>
        <div>
            <h1>Heading 3</h1>
            <h2>Heading 4</h2>
        </div>
    </body>

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




    <element name="body" 
      <zeroOrMore>
        <element name="div">
          <element name="h1">
            <text/>
          </element>
          <element name="h2">
            <text/>
          </element>
        </element>
      </zeroOrMore>
    </element>

    
    
  • index.php ( PHP script to run the validator )




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

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

Program 2:

  • data.xml




    <?xml version="1.0"?>
    <body>
        <div>
            <!--Remove Heading 1
              to violate rule-->
            <h2>Heading 2</h2>
        </div>
        <div>
            <h1>Heading 3</h1>
            <h2>Heading 4</h2>
        </div>
    </body>

    
    
  • rule.rng




    <element name="body"
      <zeroOrMore>
        <element name="div">
          <element name="h1">
            <text/>
          </element>
          <element name="h2">
            <text/>
          </element>
        </element>
      </zeroOrMore>
    </element>

    
    
  • index.php




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

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

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS