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


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


XMLReader::setRelaxNGSchemaSource()函数是PHP中的内置函数,用于设置包含RelaxNG Schema的数据以用于验证。 setRelaxNGSchemaSource()函数不同于setRelaxNGSchema()函数,因为前者将规则接受为字符串变量,而后者将规则接受为.rng文件。

用法:

bool XMLReader::setRelaxNGSchemaSource( string $source )

参数:此函数接受单个参数$source,该参数包含包含RelaxNG Schema的字符串。



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

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

范例1:

  • data.xml
    <?xml version="1.0"?> 
    <body> 
        <div> 
            <h1>GeeksForGeeks</h1> 
            <p>Portal for Geeks</p> 
        </div> 
        <div> 
            <h1>Heading 3</h1> 
            <p>Heading 4</p> 
        </div> 
    </body>
  • index.php
    <?php 
      
    // Create a new XMLReader instance 
    $XMLReader = new XMLReader(); 
      
    // Open the XML file 
    $XMLReader->open('data.xml'); 
      
    // Create rule as a string 
    $RNG = "<element name=\"body\" 
    xmlns=\"http://relaxng.org/ns/structure/1.0\"> 
    <zeroOrMore> 
      <element name=\"div\"> 
        <element name=\"h1\"> 
          <text/> 
        </element> 
        <element name=\"p\"> 
          <text/> 
        </element> 
      </element> 
    </zeroOrMore> 
    </element>"; 
      
    // Load the rule 
    $XMLReader->setRelaxNGSchemaSource($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 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> 
            <!--Create empty div element 
               to violate rule-->
        </div> 
        <div> 
            <h1>Heading 3</h1> 
            <h2>Heading 4</h2> 
        </div> 
    </body>
  • index.php
    <?php 
      
    // Create a new XMLReader instance 
    $XMLReader = new XMLReader(); 
      
    // Open the XML file 
    $XMLReader->open('data.xml'); 
      
    // Create rule as a string 
    $RNG = "<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>"; 
      
    // Load the rule 
    $XMLReader->setRelaxNGSchemaSource($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!

参考: https://www.php.net/manual/en/xmlreader.setrelaxngschemasource.php




相关用法


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