DOMDocument::validate()函数是PHP中的一个内置函数,用于根据其DTD(文档类型定义)来验证文档。 DTD定义XML文件要遵循的规则或结构,如果XML文档未遵循此格式,则此函数将返回false。
用法:
bool DOMDocument::validate( void )
参数:此函数不接受任何参数。
返回值:如果文档遵循DTD或FALSE,则此函数返回TRUE。
以下示例说明了PHP中的DOMDocument::validate()函数:
范例1:
<?php 
  
// Create a new DOMDocument 
$doc = new DOMDocument; 
  
// Load the XML with DTD rule to have 
// a root element with first, second, 
// and third as its three children 
$doc->loadXML("<?xml version=\"1.0\"?> 
<!DOCTYPE root [ 
<!ELEMENT root (first, second, third)> 
<!ELEMENT first (#PCDATA)> 
<!ELEMENT second (#PCDATA)> 
<!ELEMENT third (#PCDATA)> 
]> 
  
<!-- Create a XML following the DTD --> 
<root> 
<first>Hello</first> 
<second>There</second> 
<third>World</third> 
</root>"); 
  
// Check if XML follows the DTD rule 
if ($doc->validate()) { 
    echo "This document is valid!\n"; 
} 
?>输出:
This document is valid!
范例2:
<?php 
  
// Create a new DOMDocument 
$doc = new DOMDocument; 
  
// Load the XML 
$doc->loadXML("<?xml version=\"1.0\"?> 
<!DOCTYPE root [ 
<!ELEMENT root (one, two, three)> 
<!ELEMENT one (#PCDATA)> 
<!ELEMENT two (#PCDATA)> 
<!ELEMENT three (#PCDATA)> 
]> 
<!-- Create a XML voilating the DTD --> 
  
<root> 
<one>Hello</one> 
<two>World</two> 
</root>"); 
  
if (!$doc->validate()) { 
    echo "This document is not valid!"; 
} 
?>输出:
This document is not valid!
参考: https://www.php.net/manual/en/domdocument.validate.php
相关用法
- PHP DOMDocument createDocumentFragment()用法及代码示例
- PHP DOMDocument importNode()用法及代码示例
- PHP DOMDocument getElementsByTagName()用法及代码示例
- PHP DOMDocument createAttribute()用法及代码示例
- PHP DOMDocument __construct()用法及代码示例
- PHP DOMDocument createEntityReference()用法及代码示例
- PHP DOMDocument createProcessingInstruction()用法及代码示例
- PHP DOMDocument createTextNode()用法及代码示例
- PHP DOMDocument saveXML()用法及代码示例
- PHP DOMDocument getElementById()用法及代码示例
- PHP DOMDocument registerNodeClass()用法及代码示例
- PHP DOMDocument xinclude()用法及代码示例
- PHP DOMDocument createCDATASection()用法及代码示例
- PHP DOMDocument schemaValidateSource()用法及代码示例
- PHP DOMDocument schemaValidate()用法及代码示例
注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | DOMDocument validate() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
