PHP registerXPathNamespace() 函數(shù)
定義和用法
registerXPathNamespace() 函數(shù)為下一次 XPath 查詢創(chuàng)建命名空間語境。
語法
class SimpleXMLElement
{
string registerXPathNamespace(prefix,ns)
}
| 參數(shù) | 描述 |
|---|---|
| prefix | 必需。規(guī)定命名空間前綴。 |
| ns | 必需。規(guī)定命名空間 URL。必須匹配 XML 文檔中的命名空間。 |
例子
XML 文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <note xmlns:b="http://www.dhdzp.com/example/"> <to>George</to> <from>John</from> <heading>Reminder</heading> <b:body>Don't forget the meeting!</b:body> </note>
PHP 代碼:
<?php
$xml = simplexml_load_file("test.xml");
$xml->registerXPathNamespace("msg", "http://www.dhdzp.com/example/");
$result = $xml->xpath("msg:body");
foreach ($result as $message)
{
echo $message;
}
?>
輸出類似:
Don't forget the meeting!