PHP | DOMXPath registerNamespace()函数
DOMXPath::registerNamespace()函数是PHP中的一个内置函数,用于注册 namespaceURI 和 DOMXPath 对象的前缀。
句法:
bool DOMXPath::registerNamespace( string $prefix,
string $namespaceURI )
参数:该函数接受上面提到的两个参数,如下所述:
- $prefix:指定前缀。
- $namespaceURI:它指定命名空间 URI。
返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。
下面给出的程序说明了PHP中的DOMXPath::registerNamespace()函数:
方案一:
Hello
XML;
// Load the XML
$document->loadXML($xml);
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
// Register namespace with prefix
// x and wrong URI
$xpath->registerNamespace('x',
'geeksforgeeksnew');
// Use the prefix to create a query
// to get the text in h1
$query = '//x:body/x:h1/text()';
// Execute the query
$entries = $xpath->evaluate($query);
// Count the output
echo count($entries) . "\n";
?>
输出:
0
方案二:
Hello
XML;
// Load the XML
$document->loadXML($xml);
// Create a new DOMXPath instance
$xpath = new DOMXPath($document);
// Register namespace with prefix x
$xpath->registerNamespace('x',
'geeksforgeeks');
// Use the prefix to create a query
// to get the text in h1
$query = '//x:body/x:h1/text()';
// Execute the query
$entries = $xpath->evaluate($query);
// View the text
echo $entries->item(0)->nodeValue . "\n";
?>
输出:
Hello
参考: https://www. PHP.net/manual/en/domxpath.registernamespace。 PHP