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
相關用法
- PHP XMLReader XML()用法及代碼示例
- PHP XMLReader close()用法及代碼示例
- PHP XMLReader getAttributeNs()用法及代碼示例
- PHP XMLReader open()用法及代碼示例
- PHP XMLReader getParserProperty()用法及代碼示例
- PHP XMLReader moveToFirstAttribute()用法及代碼示例
- PHP XMLReader moveToNextAttribute()用法及代碼示例
- PHP XMLReader readInnerXml()用法及代碼示例
- PHP XMLReader readOuterXml()用法及代碼示例
- PHP XMLReader readString()用法及代碼示例
- PHP XMLReader setRelaxNGSchema()用法及代碼示例
- PHP XMLReader setParserProperty()用法及代碼示例
- PHP XMLReader setSchema()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader setRelaxNGSchemaSource() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。