📜  PHP | DOMXPath __construct()函数

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

PHP | DOMXPath __construct()函数

DOMXPath::__construct()函数是PHP中的一个内置函数,用于创建 DOMXPath 的实例。

句法:

bool DOMXPath::__construct( DOMDocument $doc )

参数:此函数接受单个参数$doc ,该参数保存与 DOMXPath 关联的 DOMDocument。

下面的例子说明了PHP中的DOMXPath::__construct()函数

示例 1:



  Hello World

XML;
  
// Load the XML
$document->loadXML($xml);
  
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
  
// Get the element
$tbody = $document->
getElementsByTagName('content')->item(0);
  
// Get the element with name content
$query = '//content';
  
// Evaluate the query
$entries = $xpath->evaluate($query, $tbody);
echo $entries[0]->nodeValue;
?>

输出:

Hello World

示例 2:



    
        First
    
    
        Second
    
    
        Third
    

XML;
  
// Load the XML
$document->loadXML($xml);
  
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
  
// Get the root element
$tbody = $document->
getElementsByTagName('root')->item(0);
  
// Count the number of element with 
// name content
$query = 'count(//content)';
  
// Evaluate the query
$entries = $xpath->evaluate($query, $tbody);
echo $entries;
?>

输出:

3

参考: https://www. PHP.net/manual/en/domxpath.construct。 PHP