Friday, October 24, 2025
HomeLanguagesPHP | XMLReader setRelaxNGSchemaSource() Function

PHP | XMLReader setRelaxNGSchemaSource() Function

The XMLReader::setRelaxNGSchemaSource() function is an inbuilt function in PHP which is used to set the data containing a RelaxNG Schema to use for validation. The setRelaxNGSchemaSource() function is different from setRelaxNGSchema() function as the former accepts the rule as a string variable whereas later function accepts the rule as a .rng file.

Syntax:

bool XMLReader::setRelaxNGSchemaSource( string $source )

Parameters: This function accepts a single parameter $source which holds the string containing the RelaxNG Schema.

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

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

Example 1:

  • data.xml




    <?xml version="1.0"?>
    <body>
        <div>
            <h1>GeeksForGeeks</h1>
            <p>Portal for Geeks</p>
        </div>
        <div>
            <h1>Heading 3</h1>
            <p>Heading 4</p>
        </div>
    </body>

    
    
  • index.php




    <?php
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Open the XML file
    $XMLReader->open('data.xml');
      
    // Create rule as a string
    $RNG = "<element name=\"body\"
    xmlns=\"http://relaxng.org/ns/structure/1.0\">
    <zeroOrMore>
      <element name=\"div\">
        <element name=\"h1\">
          <text/>
        </element>
        <element name=\"p\">
          <text/>
        </element>
      </element>
    </zeroOrMore>
    </element>";
      
    // Load the rule
    $XMLReader->setRelaxNGSchemaSource($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 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!

Example 2:

  • data.xml




    <?xml version="1.0"?>
    <body>
        <div>
            <!--Create empty div element
               to violate rule-->
        </div>
        <div>
            <h1>Heading 3</h1>
            <h2>Heading 4</h2>
        </div>
    </body>

    
    
  • index.php




    <?php
      
    // Create a new XMLReader instance
    $XMLReader = new XMLReader();
      
    // Open the XML file
    $XMLReader->open('data.xml');
      
    // Create rule as a string
    $RNG = "<element name=\"body\"
    xmlns=\"http://relaxng.org/ns/structure/1.0\">
    <zeroOrMore>
      <element name=\"div\">
        <element name=\"h1\">
          <text/>
        </element>
        <element name=\"h2\">
          <text/>
        </element>
      </element>
    </zeroOrMore>
    </element>";
      
    // Load the rule
    $XMLReader->setRelaxNGSchemaSource($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!

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS