📜  PHP | XMLReader setRelaxNGSchema()函数

📅  最后修改于: 2022-05-13 01:56:26.839000             🧑  作者: Mango

PHP | XMLReader setRelaxNGSchema()函数

XMLReader::setRelaxNGSchema()函数是PHP中的一个内置函数,用于设置用于验证的 RelaxNG Schema 的文件名或 URI。

句法:

bool XMLReader::setRelaxNGSchema( string $filename )

参数:此函数接受单个参数$filename ,其中包含指向 RelaxNG 模式的文件名或 URI。

返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。

下面的示例说明了PHP中的XMLReader::setRelaxNGSchema()函数

    示例 1:
  • data.xml (要验证的 XML 文件)
    
    
        
            

    Heading 1

            

    Heading 2

        
        
            

    Heading 3

            

    Heading 4

        
  • rule.rng (XML 文件要遵循的规则)
    
      
        
          
            
          
          
            
          
        
      
    
    
  • 指数。 PHP (运行验证器的PHP脚本)
    open('data.xml');
      
    // Load the rule file
    $XMLReader->setRelaxNGSchema('rule.rng');
      
    // Iterate through the XML nodes
    // and validate each node
    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!

方案二:

  • 数据.xml
    
    
        
                     

    Heading 2

        
        
            

    Heading 3

            

    Heading 4

        
  • 规则.rng
    
      
        
          
            
          
          
            
          
        
      
    
    
  • 指数。 PHP
    open('data.xml');
      
    // Load the rule file
    $XMLReader->setRelaxNGSchema('rule.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!
    This document is not valid!

参考: https://www. PHP.net/manual/en/xmlreader.setrelaxngschema。 PHP