PHP | DOMDocument relaxNGValidate() Function

The DOMDocument::relaxNGValidate() function is an inbuilt function in PHP which is used to performs relaxNG validation on the document. The relaxNG is an alternative to DDT and defines a structure which needs to be followed by the XML document.

Syntax:

bool DOMDocument::relaxNGValidate( string $filename )

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

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

Below given programs illustrate the DOMDocument::relaxNGValidate() function in PHP:



Program 1:

  • File name: rule.rng
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <element name="college" 
      <zeroOrMore>
        <element name="rollno">
          <element name="name">
            <text/>
          </element>
          <element name="subject">
            <text/>
          </element>
        </element>
      </zeroOrMore>
    </element>

    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\"?>
    <college>
      <rollno>
        <name>John Smith</name>
        <subject>Web</subject>
      </rollno>
      <rollno>
        <name>John Doe</name>
        <subject>CSE</subject>
      </rollno>
    </college>");
      
    // Check if XML follows the relaxNG rule
    if ($doc->relaxNGValidate('rule.rng')) {
        echo "This document is valid!\n";
    }
    ?>

    chevron_right

    
    

  • Output:
    This document is valid!

Program 2:

  • File name: rule.rng
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <element name="company" 
      <zeroOrMore>
        <element name="employee">
          <element name="name">
            <text/>
          </element>
          <element name="salary">
            <text/>
          </element>
        </element>
      </zeroOrMore>
    </element>

    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\"?>
    <company>
      <employee>
        <name>John Smith</name>
        <salary>Web</salary>
      </employee>
      <employee>
        <!-- Do not add salary to voilate rule -->
        <name>John Doe</name>
      </employee>
    </company>");
      
    // Check if XML doesn't follows the relaxNG rule
    if (!$doc->relaxNGValidate('rule.rng')) {
        echo "This document is not valid!\n";
    }
    ?>

    chevron_right

    
    

  • Output:
    This document is not valid!

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