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