DOMDocument::relaxNGValidate()函數是PHP中的內置函數,用於對文檔執行RelaxNG驗證。 RelaxNG是DDT的替代方法,它定義了XML文檔必須遵循的結構。
用法:
bool DOMDocument::relaxNGValidate( string $filename )
參數:該函數接受一個包含RNG文件的參數$filename。
返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。
下麵給出的程序說明了PHP中的DOMDocument::relaxNGValidate()函數:
程序1:
- 檔案名稱:rule.rng
<element name="college" xmlns="http://relaxng.org/ns/structure/1.0"> <zeroOrMore> <element name="rollno"> <element name="name"> <text/> </element> <element name="subject"> <text/> </element> </element> </zeroOrMore> </element>
- 檔案名稱:index.php
<?php // Create a new DOMDocument $doc = new DOMDocument; // Load the XML $doc->loadXML("<?xml version=\"1.0\"?> <college> <rollno> <name>John Smith</name> <subject>Web</subject> </rollno> <rollno> <name>John Doe</name> <subject>CSE</subject> </rollno> </college>"); // Check if XML follows the relaxNG rule if ($doc->relaxNGValidate('rule.rng')) { echo "This document is valid!\n"; } ?>
- 輸出:
This document is valid!
程序2:
- 檔案名稱:rule.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>
- 檔案名稱:index.php
<?php // Create a new DOMDocument $doc = new DOMDocument; // 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->relaxNGValidate('rule.rng')) { echo "This document is not valid!\n"; } ?>
- 輸出:
This document is not valid!
參考: https://www.php.net/manual/en/domdocument.relaxngvalidate.php
相關用法
- PHP DOMDocument createElementNS()用法及代碼示例
- PHP DOMDocument getElementsByTagName()用法及代碼示例
- PHP DOMDocument loadXML()用法及代碼示例
- PHP DOMDocument save()用法及代碼示例
- PHP DOMDocument schemaValidate()用法及代碼示例
- PHP DOMDocument load()用法及代碼示例
- PHP DOMDocument loadHTML()用法及代碼示例
- PHP DOMDocument loadHTMLFile()用法及代碼示例
- PHP DOMDocument normalizeDocument()用法及代碼示例
- PHP DOMDocument saveHTMLFile()用法及代碼示例
- PHP DOMDocument createProcessingInstruction()用法及代碼示例
- PHP DOMDocument relaxNGValidateSource()用法及代碼示例
- PHP DOMDocument createEntityReference()用法及代碼示例
- PHP DOMDocument saveHTML()用法及代碼示例
- PHP DOMDocument importNode()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | DOMDocument relaxNGValidate() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。