DOMDocument::relaxNGValidateSource()函數是PHP中的一個內置函數,用於使用字符串作為RNG架構對文檔執行RelaxNG驗證。 relaxNGValidate()和relaxNGValidateSource()之間的區別在於,前者接受rng模式文件名,而後者也接受rng模式文件名。
用法:
bool DOMDocument::relaxNGValidateSource( string $source )
參數:該函數接受一個包含RNG模式的單個參數$source。
返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。
下麵給出的程序說明了PHP中的DOMDocument::relaxNGValidateSource()函數:
程序1:
<?php
// Create a new DOMDocument
$doc = new DOMDocument;
// RNG schema
$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 XML
$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>");
// Check if XML follows the relaxNG rule
if ($doc->relaxNGValidateSource($RNG)) {
echo "This document is valid!\n";
}
?>
輸出:
This document is valid!
程序2:
<?php
// Create a new DOMDocument
$doc = new DOMDocument;
// RNG schema
$RNG = "<element name=\"company\"
xmlns=\"http://relaxng.org/ns/structure/1.0\">
<zeroOrMore>
<element name=\"employee\">
<element name=\"name\">
<text/>
</element>
<element name=\"salary\">
<text/>
</element>
</element>
</zeroOrMore>
</element>";
// 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->relaxNGValidateSource($RNG)) {
echo "This document is not valid!\n";
}
?>
輸出:
This document is not valid!
參考: https://www.php.net/manual/en/domdocument.relaxngvalidatesource.php
相關用法
- PHP DOMDocument getElementsByTagName()用法及代碼示例
- PHP DOMDocument importNode()用法及代碼示例
- PHP DOMDocument loadXML()用法及代碼示例
- PHP DOMDocument createElementNS()用法及代碼示例
- PHP DOMDocument load()用法及代碼示例
- PHP DOMDocument loadHTML()用法及代碼示例
- PHP DOMDocument loadHTMLFile()用法及代碼示例
- PHP DOMDocument normalizeDocument()用法及代碼示例
- PHP DOMDocument save()用法及代碼示例
- PHP DOMDocument createProcessingInstruction()用法及代碼示例
- PHP DOMDocument createEntityReference()用法及代碼示例
- PHP DOMDocument createTextNode()用法及代碼示例
- PHP DOMDocument saveHTML()用法及代碼示例
- PHP DOMDocument saveHTMLFile()用法及代碼示例
- PHP DOMDocument saveXML()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | DOMDocument relaxNGValidateSource() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。