DOMDocument::schemaValidateSource()函数是PHP中的一个内置函数,用于基于给定字符串中定义的模式来验证文档。 schemaValidate()和schemaValidateSource()之间的区别在于,前者可以接受模式文件名,而后者可以接受模式作为字符串。
用法:
bool DOMDocument::schemaValidateSource( string $source, int $flags = 0 )
参数:该函数接受上述和以下所述的两个参数:
- $source:它指定包含模式的字符串。
- $flags (Optional):它指定验证标志。
返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。
下面给出的程序说明了PHP中的DOMDocument::schemaValidateSource()函数:
程序1:
<?php 
  
// Create a new DOMDocument 
$doc = new DOMDocument; 
  
// XSD schema 
$XSD = "<?xml version=\"1.0\"?> 
<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" 
elementFormDefault=\"qualified\"> 
    <xs:element name=\"body\"> 
        <xs:complexType> 
            <xs:sequence> 
                <xs:element name=\"h1\" type=\"xs:string\"/> 
                <xs:element name=\"strong\" type=\"xs:integer\"/> 
            </xs:sequence> 
        </xs:complexType> 
    </xs:element> 
</xs:schema>"; 
  
// Load the XML 
$doc->loadXML("<?xml version=\"1.0\"?> 
<body> 
    <h1> Hello </h1> 
    <strong> 22 </strong> 
</body>"); 
  
// Check if XML follows the schema rule 
if ($doc->schemaValidateSource($XSD)) { 
    echo "This document is valid!\n"; 
} 
?>输出:
This document is valid!
程序2:
<?php 
  
// Create a new DOMDocument 
$doc = new DOMDocument; 
  
// RNG schema 
$XSD = "<?xml version=\"1.0\"?> 
<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" 
elementFormDefault=\"qualified\"> 
    <xs:element name=\"student\"> 
        <xs:complexType> 
            <xs:sequence> 
                <xs:element name=\"name\" type=\"xs:string\"/> 
                <xs:element name=\"rollno\" type=\"xs:integer\"/> 
            </xs:sequence> 
        </xs:complexType> 
    </xs:element> 
</xs:schema>"; 
  
// Load the XML 
$doc->loadXML("<?xml version=\"1.0\"?> 
<student>  
    <!-- rollnow element is missing here --> 
    <name> XYZ </name> 
</student> 
"); 
  
// Check if XML follows the relaxNG rule 
if (!$doc->schemaValidateSource($XSD)) { 
    echo "This document is not valid!\n"; 
} 
?>输出:
This document is not valid!
参考: https://www.php.net/manual/en/domdocument.schemavalidatesource.php
相关用法
- PHP DOMDocument load()用法及代码示例
- PHP DOMDocument loadXML()用法及代码示例
- PHP DOMDocument save()用法及代码示例
- PHP DOMDocument getElementsByTagName()用法及代码示例
- PHP DOMDocument loadHTML()用法及代码示例
- PHP DOMDocument loadHTMLFile()用法及代码示例
- PHP DOMDocument normalizeDocument()用法及代码示例
- PHP DOMDocument schemaValidate()用法及代码示例
- PHP DOMDocument saveHTML()用法及代码示例
- PHP DOMDocument createProcessingInstruction()用法及代码示例
- PHP DOMDocument relaxNGValidate()用法及代码示例
- PHP DOMDocument relaxNGValidateSource()用法及代码示例
- PHP DOMDocument createEntityReference()用法及代码示例
- PHP DOMDocument saveHTMLFile()用法及代码示例
- PHP DOMDocument importNode()用法及代码示例
注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | DOMDocument schemaValidateSource() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
