当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP XMLReader setRelaxNGSchema()用法及代码示例


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

用法:

bool XMLReader::setRelaxNGSchema( string $filename )

参数:该函数接受单个参数$filename,该参数包含指向RelaxNG Schema的文件名或URI。



返回值:如果成功,此函数将返回TRUE,否则将返回FALSE。

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

    范例1:
  • data.xml(要验证的XML文件)
    <?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>
  • 规则(XML文件要遵循的规则)
    <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>
  • index.php(PHP脚本运行验证器)
    <?php 
      
    // Create a new XMLReader instance 
    $XMLReader = new XMLReader(); 
      
    // Open the XML file 
    $XMLReader->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!<br>"; 
            } 
      
        } 
    } 
    ?>
  • 输出:
    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!

程序2:

  • data.xml
    <?xml version="1.0"?> 
    <body> 
        <div> 
            <!--Remove Heading 1 
              to violate rule-->
            <h2>Heading 2</h2> 
        </div> 
        <div> 
            <h1>Heading 3</h1> 
            <h2>Heading 4</h2> 
        </div> 
    </body>
  • 规则
    <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>
  • index.php
    <?php 
      
    // Create a new XMLReader instance 
    $XMLReader = new XMLReader(); 
      
    // Open the XML file 
    $XMLReader->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!<br>"; 
            } 
      
        } 
    } 
    ?>
  • 输出:
    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




相关用法


注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | XMLReader setRelaxNGSchema() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。