XMLReader::setRelaxNGSchemaSource()函数是PHP中的内置函数,用于设置包含RelaxNG Schema的数据以用于验证。 setRelaxNGSchemaSource()函数不同于setRelaxNGSchema()函数,因为前者将规则接受为字符串变量,而后者将规则接受为.rng文件。
用法:
bool XMLReader::setRelaxNGSchemaSource( string $source )
参数:此函数接受单个参数$source,该参数包含包含RelaxNG Schema的字符串。
返回值:如果成功,此函数将返回TRUE,否则将返回FALSE。
以下示例说明了PHP中的XMLReader::setRelaxNGSchemaSource()函数:
范例1:
data.xml
GeeksForGeeks
Portal for Geeks
index.php
// Create a new XMLReader instance
$XMLReader = new XMLReader();
// Open the XML file
$XMLReader->open('data.xml');
// Create rule as a string
$RNG = "
xmlns=\"http://relaxng.org/ns/structure/1.0\">
";
// 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!
";
}
}
}
?>
输出:
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!
范例2:
data.xml
Heading 3
Heading 4
index.php
// Create a new XMLReader instance
$XMLReader = new XMLReader();
// Open the XML file
$XMLReader->open('data.xml');
// Create rule as a string
$RNG = "
xmlns=\"http://relaxng.org/ns/structure/1.0\">
";
// 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!
";
}
}
}
?>
输出:
This document is not valid!
This document is not valid!
This document is not valid!