PHP | DOMDocument xinclude() Function

The DOMDocument::xinclude() function is an inbuilt function in PHP which is used to substitute the XIncludes in a DOMDocument Object.

Syntax:

int DOMDocument::xinclude( int $options = 0 )

Parameters: This function accepts an optional single parameter $options which holds the libxml parameter.

Return Value: This function returns the number of XIncludes in the document, -1 if some processing failed, or FALSE if there were no substitutions.

Below examples illustrate the DOMDocument::xinclude() function in PHP:



Example 1: In this program, we will include from XML from new.xml to index.php.

  • new.xml
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version='1.0'?>
    <disclaimer>
      <p>
        This is the content from new.xml
        included using xinclude.
      </p>
    </disclaimer>

    chevron_right

    
    

  • index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    $xml = <<<EOD
    <?xml version="1.0" ?>
      <xi:include href="new.xml">
      </xi:include>
    </root>
    EOD;
      
    // Create a new DOMDocument
    $dom = new DOMDocument();
      
    // let's have a nice output
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
      
    // load the XML string defined above
    $dom->loadXML($xml);
      
    // substitute xincludes
    $dom->xinclude();
      
    echo $dom->saveXML();
    ?>

    chevron_right

    
    

  • Output:
    This is the content from new.xml included using xinclude.

Example 2: In this program, we will add a fallback if xinclude fails.

  • index.php
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    $xml = <<<EOD
    <?xml version="1.0" ?>
      <xi:include href="wrong_name.xml">
       <xi:fallback>
        <error>xml not found</error>
       </xi:fallback>
      </xi:include>
    </root>
    EOD;
      
    // Create a new DOMDocument
    $dom = new DOMDocument();
      
    // let's have a nice output
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
      
    // load the XML string defined above
    $dom->loadXML($xml);
      
    // substitute xincludes
    $dom->xinclude();
      
    echo $dom->saveXML();
    ?>

    chevron_right

    
    

  • Output:
    xml not found

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