PHP | DOMDocument relaxNGValidateSource() Function
Last Updated :
12 Apr, 2022
The DOMDocument::relaxNGValidateSource() function is an inbuilt function in PHP which is used to perform relaxNG validation on the document using a string as RNG schema. The difference between relaxNGValidate() and relaxNGValidateSource() is that the former accepts a rng schema filename whereas latter can accept a rng schema as string too.
Syntax:
bool DOMDocument::relaxNGValidateSource( string $source )
Parameters: This function accepts a single parameter $source which holds the RNG schema.
Return Value: This function returns TRUE on success or FALSE on failure.
Below given programs illustrate the DOMDocument::relaxNGValidateSource() function in PHP:
Program 1:
php
<?php
$doc = new DOMDocument;
$RNG = "<element name=\"body\"
xmlns=\"http:
<zeroOrMore>
<element name=\"div\">
<element name=\"h1\">
<text/>
</element>
<element name=\"h2\">
<text/>
</element>
</element>
</zeroOrMore>
</element>";
$doc ->loadXML("<?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>");
if ( $doc ->relaxNGValidateSource( $RNG )) {
echo "This document is valid!\n";
}
?>
|
Output:
This document is valid!
Program 2:
php
<?php
$doc = new DOMDocument;
$RNG = "<element name=\"company\"
xmlns=\"http:
<zeroOrMore>
<element name=\"employee\">
<element name=\"name\">
<text/>
</element>
<element name=\"salary\">
<text/>
</element>
</element>
</zeroOrMore>
</element>";
$doc ->loadXML("<?xml version=\"1.0\"?>
<company>
<employee>
<name>John Smith</name>
<salary>Web</salary>
</employee>
<employee>
<!-- Do not add salary to violate rule -->
<name>John Doe</name>
</employee>
</company>");
if (! $doc ->relaxNGValidateSource( $RNG )) {
echo "This document is not valid!\n";
}
?>
|
Output:
This document is not valid!
Reference: https://www.php.net/manual/en/domdocument.relaxngvalidatesource.php
Share your thoughts in the comments
Please Login to comment...